You might want to have a special icon for your control appear in the Toolbox. You can specify a particular image by using the ToolboxBitmapAttribute Class. This class is an attribute, a special kind of class you can attach to other classes. For more information on attributes, see Attributes Overview for Visual Basic and Introduction to Attributes for C#.
Using the ToolboxBitmapAttribute, you can specify a String that indicates the path and file name for a 16 by 16 pixel bitmap. This bitmap then appears next to your control when added to the Toolbox. You can also specify a Type, in which case the bitmap associated with that type is loaded. If you specify both a Type and a String, the control searches for an image resource with the name specified by the String parameter in the assembly containing the type specified by the Type parameter.
To specify a Toolbox bitmap for your control
- Add the ToolBoxBitmapAttribute to the class declaration of your control before the Class keyword for Visual Basic, and above the class declaration for Visual C#.
' Visual Basic ' 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 // C# // 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 { }
See Also
Control Authoring for Windows Forms | Visual Basic .NET Attributes | Introduction to Attributes