Add (ToolBoxItems Collection)
Collapse the table of content
Expand the table of content
This documentation is archived and is not being maintained.

Add Method (ToolBoxItems Collection)

Visual Studio .NET 2003

Creates a new item and adds it to the ToolBox.

[Visual Basic .NET]
Public Function Add( _
   ByVal Name As String, _
   ByVal Data As Variant, _
   Optional ByVal Format As vsToolBoxItemFormat = vsToolBoxItemFormatText _
) As ToolBoxItem
[Visual Basic 6]
Function Add( _
   ByVal Name As String, _
   ByVal Data As Variant, _
   Optional ByVal Format As vsToolBoxItemFormat = vsToolBoxItemFormatText _
) As ToolBoxItem
HRESULT __stdcall Add(
   BSTR Name,
   VARIANT Data,
   vsToolBoxItemFormat Format,
   /* [out, retval] */ ToolBoxItem** retVal
ToolBoxItem Add(
   string Name,
   object Data,
   vsToolBoxItemFormat Format
[JScript .NET]
public 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.


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 name passed can be in either of the following formats:

  • <AssemblyPath> — Where "<AssemblyPath>" is a filename 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> — This is 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 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 objToolbox As ToolBox = win.Object
   Dim colTbxTabs As ToolBoxTabs

   ' Create an object reference to the ToolBoxTabs object.
   colTbxTabs = objToolbox.ToolBoxTabs

   ' Add a new Text item to the first tab in the Toolbox.
   colTbxTabs.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 using a path to the file. The component to be added must be a .NET control, such as a Visual Basic .NET Windows Control Library component.

Sub ToolBoxItemAddExample2()
      Dim objToolbox As ToolBox
      objToolbox = CType(DTE.Windows.Item(Constants. _
        vsWindowKindToolbox).Object, EnvDTE.ToolBox)
      ' Create a new tab called "My Controls."
      Dim objToolBoxTab As ToolBoxTab = objToolbox.ToolBoxTabs. _
        Add("My Controls")
      ' Set focus on 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.
      objToolBoxTab.ToolBoxItems.Add("MyDotNetControl", "C:\Libraries\ _
        ADotNetControl.dll", vsToolBoxItemFormat. _
   Catch e As System.Exception
      ' Display any errors that occur.
   End Try
End Sub

See Also

Applies To: ToolBoxItems Collection

© 2016 Microsoft