Amr ElSehemy.
Menu
2008.01.08

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.