How to: Provide a Toolbox Bitmap for a Control


If you want to have a special icon for your control appear in the Toolbox, you can specify a particular image by using the ToolboxBitmapAttribute. This class is an attribute, a special kind of class you can attach to other classes. For more information about attributes, see NOT IN BUILD: Attributes Overview in Visual Basic for Visual Basic and Attributes for Visual 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

  1. 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#.

    // Specifies the bitmap associated with the Button type.  
    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  
  2. Rebuild the project.

    System_CAPS_ICON_note.jpg Note

    The bitmap does not appear in the Toolbox for autogenerated controls and components. To see the bitmap, reload the control by using the Choose Toolbox Items dialog box. For more information, see Walkthrough: Automatically Populating the Toolbox with Custom Components.

Walkthrough: Automatically Populating the Toolbox with Custom Components
Developing Windows Forms Controls at Design Time
Attributes (Visual Basic)