Share via


ToolBoxItems.Add 方法

创建新项并将其添加到**“工具箱”**中。

命名空间:  EnvDTE
程序集:  EnvDTE(在 EnvDTE.dll 中)

语法

声明
Function Add ( _
    Name As String, _
    Data As Object, _
    Format As vsToolBoxItemFormat _
) As ToolBoxItem
ToolBoxItem Add(
    string Name,
    Object Data,
    vsToolBoxItemFormat Format
)
ToolBoxItem^ Add(
    [InAttribute] String^ Name, 
    [InAttribute] Object^ Data, 
    [InAttribute] vsToolBoxItemFormat Format
)
abstract Add : 
        Name:string * 
        Data:Object * 
        Format:vsToolBoxItemFormat -> ToolBoxItem
function Add(
    Name : String, 
    Data : Object, 
    Format : vsToolBoxItemFormat
) : ToolBoxItem

参数

  • Name
    类型:String

    必选。 表示新项标题的字符串。

  • Data
    类型:Object

    必选。 一个表示要添加到**“工具箱”**中的字符串、控件或其他项的变量。

返回值

类型:EnvDTE.ToolBoxItem
一个 ToolBoxItem 对象。

备注

如果 ToolBoxItems 集合属于一个已删除的 ToolBoxTab 对象,或者如果尝试添加一个名称已存在的选项卡,则此方法失败。

对于 Data 参数,Visual C++ 用户可传递 IDataObject 的 IUnknown。

添加 Format 类型的程序集 vsToolBoxItemFormatDotNETComponent 时,传递给 Data 参数的值可以采用下面两种格式中的任意一种:

  • <AssemblyPath>,其中“<AssemblyPath>”是指向托管程序集的路径和文件名,如 C:\Libraries\MyAssembly.dll。 在使用此格式时,该 .DLL 中的所有类都添加到**“工具箱”**中。

  • <AssemblyNameInTheGAC>,这是一个作为程序集限定的引用列出的单个类。 单个类可以作为控件添加,只要它们是对全局程序集缓存 (GAC) 中的程序集的引用,例如:WindowControlLibrary1.UserControl1, WindowControlLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=<Your Token>。(您应将 <Your Token> 替换为您的公钥标记,在将您的程序集添加到 GAC 中时需要使用这一公钥标记。)

示例

下面的示例演示了如何向**“工具箱”**添加文本文档:

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

下面的示例演示了如何使用此文件的路径向**“工具箱”**添加 .NET 组件。 添加的组件必须是一个 .NET 控件,例如某个 Visual Basic Windows 控件库组件。

Sub ToolBoxItemAddExample2()
    Try
        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.
        tlBoxTab.Activate()
        ' 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(", _
        vsToolBoxItemFormat.vsToolBoxItemFormatDotNETComponent)
    Catch ex As System.Exception
        ' Display any errors that occur.
        MsgBox("ERROR: " & ex.Message)
    End Try
End Sub

.NET Framework 安全性

请参阅

参考

ToolBoxItems 接口

EnvDTE 命名空间

其他资源

如何:控制工具箱