ToolBoxItems.Add Method

Creates a new item and adds it to the ToolBox.

Namespace: EnvDTE
Assembly: EnvDTE (in envdte.dll)

ToolBoxItem Add (
	[InAttribute] string Name,
	[InAttribute] Object Data,
	[OptionalAttribute] [InAttribute] vsToolBoxItemFormat Format
ToolBoxItem Add (
	/** @attribute InAttribute() */ String Name, 
	/** @attribute InAttribute() */ Object Data, 
	/** @attribute InAttribute() */ /** @attribute OptionalAttribute() */ vsToolBoxItemFormat Format
function Add (
	Name : String, 
	Data : Object, 
	Format : vsToolBoxItemFormat
) : ToolBoxItem



Required. A string representing the caption of the new item.


Required. A variant representing the string, control, or other item to be added to the ToolBox.


Optional. A vsToolBoxItemFormat constant indicating the format of the new item.

Return Value

A ToolBoxItem object.

This method fails if the ToolBoxItems collection belongs to a ToolBoxTab object that has been deleted, or if you attempt to add a tab with a name that already exists.

For the Data argument, Visual C++ users can pass the IUnknown of the IDataObject.

When adding an assembly of Format type vsToolBoxItemFormatDotNETComponent, the value passed to the Name parameter can be in either of the following formats:

  • <AssemblyPath>—Where "<AssemblyPath>" is a path and file name pointing to a managed assembly, such as C:\Libraries\MyAssembly.dll. When you use this format, all classes within the .DLL are added to the ToolBox.

  • <AssemblyNameInTheGAC>—A single class listed as an assembly-qualified reference. Single classes can be added as controls, provided they are references to an assembly that is in the global assembly cache (GAC), such as: WindowControlLibrary1.UserControl1, WindowControlLibrary, Version=, Culture=neutral, PublicKeyToken=<Your Token>. (You would replace <Your Token> with your public key token, which is required to add your assembly to the GAC.)

The following example demonstrates how to add a text document to the ToolBox:

Sub ToolBoxAddExample1()
    ' This adds a Text item to the first tab of the ToolBox.
    ' Define the variables and create an object reference to the IDE's  
    ' ToolBox object.
    Dim win As Window = DTE.Windows.Item(Constants.vsWindowKindToolbox)
    Dim tlbox As ToolBox = win.Object
    Dim tbxTabs As ToolBoxTabs

    ' Create an object reference to the ToolBoxTabs object.
    tbxTabs = tlbox.ToolBoxTabs

    ' Add a new Text item to the first tab in the ToolBox.
    tbxTabs.Item(1).ToolBoxItems.Add("New Text Item", "Some text to _
    add to the document.", vsToolBoxItemFormat.vsToolBoxItemFormatText)
End Sub

The following example demonstrates how to add a .NET component to the ToolBox by using a path to the file. The component to be added must be a .NET control, such as a Visual Basic Windows Control Library component.

Sub ToolBoxItemAddExample2()
        Dim tlBox As ToolBox
        tlBox = CType(DTE.Windows.Item(Constants. _
        vsWindowKindToolbox).Object, EnvDTE.ToolBox)
        ' Create a new tab called "My Controls."
        Dim tlBoxTab As ToolBoxTab = tlBox.ToolBoxTabs. _
        Add("My Controls")
        ' Set focus to the new Toolbox tab.
        ' Add a .NET control as a new control in the new ToolBox tab. 
        ' The constant "vsToolBoxItemFormatDotNETComponent" alerts the 
        ' ToolBox to the type of control you are adding.
        tlBoxTab.ToolBoxItems.Add("MyDotNetControl", _
        "C:\Libraries\ADotNetControl.dll(", _
    Catch ex As System.Exception
        ' Display any errors that occur.
        MsgBox("ERROR: " & ex.Message)
    End Try
End Sub

Community Additions