Custom Controls Design Time Support Part 3: Adding Image in the Toolbox

Attributes Applied To Description
ToolboxBitmap Controls           Allows you to specify an icon to represent a control in a container, such as the Microsoft Visual Studio Form Designer.

» ToolboxBitmapAttribute if you want to redistribute your custom control you should use this attribute to add an icon to it.

[code:c#]

[ToolboxBitmap(typeof(Button))]
public class ButtonEx : Button

[/code]

This will show the default icon of the Windows.Forms.Button control, to add a custom icon you will use one of the other two ToolboxBitmapAttribute constructors, the first one accepts a string as the full path for the image, the second one with a string and a type this one is used more frequently.

[code:c#]

[ToolboxBitmap(typeof(ButtonEx),"ButtonEx.png")]
public class ButtonEx : Button

[/code]

Note: For this to work you need to be sure of 2 things:

  1. The image name would be exactly spelled as the type (control).
  2. Right click the image and make sure its BuildAction is Embedded Resource.

Posted

in

,

by

Tags:

Comments

3 responses to “Custom Controls Design Time Support Part 3: Adding Image in the Toolbox”

  1. Dave Avatar

    It might be worth noting that ToolboxBitmap is part of the System.Drawing namespace, not the ComponentModel namespace that a lot of the commonly used attributes are in.

  2. Amr Avatar

    Thanks for the heads up, I will take care mentioning the namespaces too.

  3. Vivek Avatar
    Vivek

    I would like to add on that the use of [ToolboxBitmap] attribute does not necessarily require the control image to have the same name as of the control(or component). The toolbox icon will be always the default one for automatically added controls to the toolbox (like in the case of a test project in a solution containing the control library project). It will change to the one that you have embedded only when you add it to the toolbox through choose items dialog.

Leave a Reply

Your email address will not be published. Required fields are marked *