共用方式為


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

    必要項。 代表要加入到 [工具箱] 的字串、控制項或其他項目的 Variant。

傳回值

類型:EnvDTE.ToolBoxItem
ToolBoxItem 物件。

備註

如果 ToolBoxItems 集合屬於已被刪除的 ToolBoxTab 物件,或是您嘗試新增名稱已存在的索引標籤,則這個方法會失敗。

針對 Data 引數,Visual C++ 使用者可以傳遞 IDataObject 的 IUnknown。

在加入 Format 型別 vsToolBoxItemFormatDotNETComponent 的組件時,傳遞至 Data 參數的值可為下列任一格式:

  • <AssemblyPath>,其中 "<AssemblyPath>" 為指向 Managed 組件的路徑與檔案名稱,例如 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 命名空間

其他資源

如何:控制工具箱