Set the icon of a control in the Toolbox (Windows Forms .NET)

Controls that you create always receive a generic icon for the Toolbox window in Visual Studio. However, when you change the icon, it adds a sense of professionalism to your control, and makes it stand out in the toolbox. This article teaches you how to set the icon for your control.

Bitmap icon

Icons for the Toolbox window in Visual Studio must conform to certain standards, otherwise they're ignored or are displayed incorrectly.

  • Size: Icons for a control must be a 16x16 bitmap image.
  • File type: The icon can be either a Bitmap (.bmp) or a Windows Icon (.ico) file.
  • Transparency: The magenta color (RGB: 255,0,255, Hex: 0xFF00FF) is rendered transparent.
  • Themes: Visual Studio has multiple themes, but each theme is considered either dark or light. Your icon should be designed for the light theme. When Visual Studio uses a dark theme, the dark and light colors in the icon are automatically inverted.

How to assign an icon

Icons are assigned to a control with the ToolboxBitmapAttribute attribute. For more information about attributes, see Attributes (C#) or Attributes overview (Visual Basic).

Tip

You can download a sample icon from GitHub.

The attribute is set on the control's class, and has three different constructors:

  • ToolboxBitmapAttribute(Type)—This constructor takes a single type reference, and from that type, tries to find an embedded resource to use as the icon.

    The type's FullName value is used to try to find a corresponding embedded resource based on the name of the type. For example, if the MyProject.MyNamespace.CompassRose type is referenced, the attribute looks for a resource named MyProject.MyNamespace.CompassRose.bmp or MyProject.MyNamespace.CompassRose.ico. If the resource is found, it's used as the control's icon.

    // Looks for a CompassRose.bmp or CompassRose.ico embedded resource in the
    // same namespace as the CompassRose type.
    [ToolboxBitmap(typeof(CompassRose))]
    public partial class CompassRose : UserControl
    {
        // Code for the control
    }
    
    ' Looks for a CompassRose.bmp or CompassRose.ico embedded resource in the
    ' same namespace as the CompassRose type.
    <ToolboxBitmap(GetType(CompassRose))>
    Public Class CompassRose
        ' Code for the control
    End Class
    
  • ToolboxBitmapAttribute(Type, String)—This constructor takes two parameters. The first parameter is a type, and the second is the namespace and name of the resource in the assembly of that type.

    // Loads the icon from the WinFormsApp1.Resources.CompassRoseIcon.bmp resource
    // in the assembly containing the type CompassRose
    [ToolboxBitmap(typeof(CompassRose), "WinFormsApp1.Resources.CompassRoseIcon.bmp")]
    public partial class CompassRose : UserControl
    {
        // Code for the control
    }
    
    ' Loads the icon from the WinFormsApp1.Resources.CompasRoseIcon.bmp resource
    ' in the assembly containing the type CompassRose
    <ToolboxBitmap(GetType(CompassRose), "WinFormsApp1.Resources.CompasRoseIcon.bmp")>
    Public Class CompassRose
        ' Code for the control
    End Class
    
  • ToolboxBitmapAttribute(String)—This constructor takes a single string parameter, the absolute path to the icon file.

    // Loads the icon from a file on disk
    [ToolboxBitmap(@"C:\Files\Resources\MyIcon.bmp")]
    public partial class CompassRose : UserControl
    {
        // Code for the control
    }
    
    ' Loads the icon from a file on disk
    <ToolboxBitmap("C:\Files\Resources\MyIcon.bmp")>
    Public Class CompassRose
        ' Code for the control
    End Class