操作說明:為控制項提供工具箱點陣圖
如果您想要讓控制項的特殊圖示出現在 Visual Studio 的 [工具箱] 中,您可以使用 ToolboxBitmapAttribute 來指定特定影像。 此類別是一個「屬性」,一種您可以附加至其他類別的特殊類別。 如需屬性的詳細資訊,請參閱 Visual Basic 的屬性概觀 (Visual Basic) 或 C# 的屬性 (C#)。
使用 ToolboxBitmapAttribute,您可以指定一個字串,表示 16 x 16 像素點陣圖的路徑和檔案名稱。 將此點陣圖新增至 [工具箱],它會接著出現在您的控制項旁邊。 您也可以指定 Type,在此情況下會載入與該類型相關聯的點陣圖。 如果您同時指定 Type 和一個字串,控制項會在包含 Type 參數所指定類型的組件中,搜尋具有字串參數所指定名稱的影像資源。
指定控制項的工具箱點陣圖
將 ToolboxBitmapAttribute 新增至控制項的類別宣告:位於
Class
關鍵字之前 (Visual Basic) 以及在類別宣告之上 (Visual C#)。' Specifies the bitmap associated with the Button type. <ToolboxBitmap(GetType(Button))> Class MyControl1 ' Specifies a bitmap file. End Class <ToolboxBitmap("C:\Documents and Settings\Joe\MyPics\myImage.bmp")> _ Class MyControl2 End Class ' Specifies a type that indicates the assembly to search, and the name ' of an image resource to look for. <ToolboxBitmap(GetType(MyControl), "MyControlBitmap")> Class MyControl End Class
// Specifies the bitmap associated with the Button type. [ToolboxBitmap(typeof(Button))] class MyControl1 : UserControl { } // Specifies a bitmap file. [ToolboxBitmap(@"C:\Documents and Settings\Joe\MyPics\myImage.bmp")] class MyControl2 : UserControl { } // Specifies a type that indicates the assembly to search, and the name // of an image resource to look for. [ToolboxBitmap(typeof(MyControl), "MyControlBitmap")] class MyControl : UserControl { }
重建專案。
注意
點陣圖並不會針對自動產生的控制項和元件出現在工具箱中。 若要查看點陣圖,請使用 [選擇工具箱項目] 對話方塊,重新載入控制項。 如需詳細資訊,請參閱逐步解說:自動將自訂元件填入工具箱。