Dela via


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 Attributes Overview in Visual Basic for Visual Basic and Attributes (C# and Visual Basic) 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.
    <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
    {
    }
    
    // Specifies the bitmap associated with the Button type.
    /** @attribute ToolboxBitmap(Button.class)  */
    class MyControl1 extends UserControl
    {
    }
    // Specifies a bitmap file.
    /** @attribute ToolboxBitmap("C:\\Documents and Settings\\Joe\\MyPics\\myImage.bmp")*/
    class MyControl2 extends UserControl
    {
    }
    // Specifies a type that indicates the assembly to search, and the name 
    // of an image resource to look for.
    /* @attribute ToolboxBitmap(MyControl.class, "MyControlBitmap") */
    class MyControl extends UserControl
    {
    }
    
  2. Rebuild the project.

    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.

See Also

Tasks

Walkthrough: Automatically Populating the Toolbox with Custom Components

Reference

Attributes (C# and Visual Basic)

ToolboxBitmapAttribute

Concepts

Attributes (Visual Basic)

Other Resources

Developing Windows Forms Controls at Design Time