ToolBoxItems.Add(String, Object, vsToolBoxItemFormat) 方法

定义

创建新项并将其添加到 " 工具箱"。

EnvDTE::ToolBoxItem Add(std::wstring const & Name, winrt::Windows::Foundation::IInspectable const & Data, EnvDTE::vsToolBoxItemFormat Format = EnvDTE.vsToolBoxItemFormat.vsToolBoxItemFormatText);
[System.Runtime.InteropServices.DispId(4)]
public EnvDTE.ToolBoxItem Add (string Name, object Data, EnvDTE.vsToolBoxItemFormat Format = EnvDTE.vsToolBoxItemFormat.vsToolBoxItemFormatText);
[<System.Runtime.InteropServices.DispId(4)>]
abstract member Add : string * obj * EnvDTE.vsToolBoxItemFormat -> EnvDTE.ToolBoxItem
Public Function Add (Name As String, Data As Object, Optional Format As vsToolBoxItemFormat = EnvDTE.vsToolBoxItemFormat.vsToolBoxItemFormatText) As ToolBoxItem

参数

Name
String

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

Data
Object

必需。 一个变量,表示要添加到 " 工具箱" 中的字符串、控件或其他项。

Format
vsToolBoxItemFormat

可选。 一个 vsToolBoxItemFormat 常数,指示新项的格式。

返回

ToolBoxItem

ToolBoxItem 对象。

属性

示例

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

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  

注解

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

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

添加类型的程序集 FormatvsToolBoxItemFormatDotNETComponent ,传递给参数的值 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 所必需的。 )

适用于