Share via


HOW TO:控制工具箱

在 Visual Studio Automation 模型中,ToolBox 物件是以下列物件和集合來表示:

物件名稱

描述

ToolBox 物件

表示 [工具箱]。

ToolBoxTabs 集合

表示 [工具箱] 中的所有索引標籤。

ToolBoxTab2 物件

表示 [工具箱] 中的一個索引標籤。

ToolBoxTab3 物件

表示 [工具箱] 中的一個索引標籤。

ToolBoxItem2 集合

包含 [工具箱] 某個索引標籤中所有項目的集合。

ToolBoxItem 物件

表示 [工具箱] 某個索引標籤中的單一項目。

使用這些物件和集合,您可以:

  • 在 [工具箱] 中加入索引標籤 (Add 方法)

  • 啟動 [工具箱] 中的索引標籤 (Activate 方法)

  • 刪除 [工具箱] 中的索引標籤 (Delete 方法)

  • 在 [工具箱] 中加入項目 (Add 方法)

  • 在 [工具箱] 中選取項目 (Select 方法)

  • 刪除 [工具箱] 某個索引標籤中的項目 (Delete 方法)

  • 將 [工作清單] 展示變更為圖示檢視或清單檢視 (ListView 屬性)

除了控制 [工具箱] 的內容外,您也可以控制其特性,例如寬度和高度。 如需詳細資訊,請參閱HOW TO:變更視窗特性

注意事項注意事項

根據目前使用的設定與版本,您所看到的對話方塊與功能表命令可能會與 [說明] 中所描述的不同。這些程序已使用現行的 [一般開發設定] 進行開發。 若要變更設定,請從 [工具] 功能表中選擇 [匯入和匯出設定]。 如需詳細資訊,請參閱 使用設定

範例

在下面這個範例中,會示範如何參考及使用 [工具箱] Automation 模型中的各個成員。 這個範例會建立新的 [工具箱] 索引標籤,在索引標籤中加入一些項目 (包括 .NET 元件),然後刪除其中一個項目。 也可以選擇性刪除新的索引標籤。 如需如何執行範例的詳細資訊,請參閱 HOW TO:編譯和執行 Automation 物件模型程式碼範例

' VSMacro
Sub ToolboxExample()
    Dim tlBox As ToolBox
    Dim tbxTabs As ToolBoxTabs
    Dim tbxTab As ToolBoxTab
    Dim tbxItems As ToolBoxItems
    Dim tbxItem As ToolBoxItem

    Try
        ' Create an object reference to the IDE's ToolBox object and
        ' its tabs.
        tlBox = DTE.Windows.Item(Constants.vsWindowKindToolbox).Object
        tbxTabs = tlBox.ToolBoxTabs

        ' Add a new tab to the Toolbox and select it.
        tbxTab = tbxTabs.Add("New ToolBox Tab")
        tbxTab.Activate()

        ' Add new items to the new Toolbox tab. This shows two
        ' different ways to index the Toolbox tabs. The third item
        ' added is a .NET component that contains a number of 
        ' Web-related controls.
        tbxTab.ToolBoxItems.Add("Text Item", "Hello world")
        tbxTab.ToolBoxItems.Add("HTML Item", "Hello world", _
        vsToolBoxItemFormat.vsToolBoxItemFormatHTML)
        ' Replace the <Path and name of a .NET dll>
        ' with a path to a .NET dll file.
        tbxTabs.Item("New Toolbox Tab").ToolBoxItems.Add _
        ("DotNET Component", "< Path and name of a .NET dll >", _
 vsToolBoxItemFormat. _
        vsToolBoxItemFormatDotNETComponent)
        
        ' Use the ToolboxItems collection to access all the items under 
        ' a ToolBox tab.
        tbxItems = tbxTab.ToolBoxItems

        ' List the number of ToolboxItems in a ToolBoxTab.
        MsgBox _
("Number of items in " & tbxTabs.Item(1).Name & " tab: "  _
& tbxItems.Count)

        ' Select the second item in the ToolboxItems collection and 
        ' delete it.
        tbxItems.Item(2).Select()
        If (MsgBox("Delete the second ToolBox item?", vbYesNo) = vbYes) _
        Then
            tbxItems.SelectedItem.Delete()
            MsgBox("Number of items in " & tbxTabs.Item(1).Name & " _
            tab: " & tbxItems.Count)
        End If
        If (MsgBox("Delete the new tab?", vbYesNo) = vbYes) Then
            tbxTabs.Item("New ToolBox Tab").Delete()
            MsgBox("Tab deleted.")
        End If
    Catch ex As System.Exception
        MsgBox("ERROR: " & ex.Message)
    End Try
End Sub
Using System.Windows.Forms;
public void OnConnection(object application,
 Extensibility.ext_ConnectMode connectMode, object addInInst, ref
 System.Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    // Pass the applicationObject member variable to the code example.
    ToolboxExample(_applicationObject); 
}

public void ToolboxExample( DTE2 dte ) 
{ 
    ToolBox tlBox = null; 
    ToolBoxTabs tbxTabs = null; 
    ToolBoxTab3 tbxTab = null; 
    ToolBoxItems tbxItems = null; 
    ToolBoxItem2 tbxItem = null; 

    try 
    { 
        // Create an object reference to the IDE's ToolBox object and
        // its tabs.
        tlBox = (ToolBox )( dte.Windows.Item(
 Constants.vsWindowKindToolbox ).Object ); 
        tbxTabs = tlBox.ToolBoxTabs; 

        // Add a new tab to the Toolbox and select it.
        tbxTab = (ToolBoxTab3)tbxTabs.Add( "New ToolBox Tab" ); 
        tbxTab.Activate(); 

        // Add new items to the new Toolbox tab. This shows two
        // different ways to index the Toolbox tabs. The third item
        // added is a .NET component that contains a number of 
        // Web-related controls.
        tbxTab.ToolBoxItems.Add( "Text Item", "Hello world",
 (EnvDTE.vsToolBoxItemFormat.vsToolBoxItemFormatText)); 
        tbxTab.ToolBoxItems.Add( "HTML Item", "Hello world"
, vsToolBoxItemFormat.vsToolBoxItemFormatHTML ); 
        // Replace the <Path and name of a .NET dll>
        // with a path to a .NET dll file.
        tbxTabs.Item( "New Toolbox Tab" ).ToolBoxItems.Add
( "DotNET Component",
 "<Path and name of a .NET dll>",
 vsToolBoxItemFormat.vsToolBoxItemFormatDotNETComponent ); 

        // Use the ToolboxItems collection to access all the 
        // items under a ToolBox tab.
        tbxItems = tbxTab.ToolBoxItems; 

        // List the number of ToolboxItems in a ToolBoxTab.
        MessageBox.Show( "Number of items in " +
 tbxTabs.Item( 1 ).Name + " tab: " + tbxItems.Count); 
          
        // Select the second item in the ToolboxItems collection and 
        // delete it.
        // Comment the following lines out, if you do not want to
        // delete the controls.
        tbxItems.Item( 2 ).Select(); 
        
        tbxItems.SelectedItem.Delete(); 
        MessageBox.Show( "Number of items in " 
+ tbxTabs.Item( 1 ).Name + " tab: " + tbxItems.Count); 
        tbxTabs.Item( "New ToolBox Tab" ).Delete(); 
        MessageBox.Show( "Tab deleted."); 
        
    } 
    catch ( System.Exception ex ) 
    { 
        MessageBox.Show( "ERROR: " + ex.Message); 
    } 
}

安全性

如果加入的 COM 物件必須登錄至 [工具箱],將會嘗試登錄 COM 元件。 如果您不是以系統管理員 (或系統管理員群組成員) 的身分登入,登錄將會失敗,而且不會將 COM 物件加入至 [工具箱]。

不論您的權限層級為何,您都不能在 [工具箱] 中瀏覽及加入未登錄的 COM 元件。

請參閱

工作

HOW TO:變更視窗特性

HOW TO:建立增益集

逐步解說:建立精靈

概念

Automation 物件模型圖表

其他資源

建立和控制環境視窗

建立增益集和精靈

Automation 與擴充性參考