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

by Amr Elsehemy 8. January 2008 02:15
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.

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

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.

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

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.

Tags: , ,

Design Time Support

Comments

1/15/2008 3:53:16 PM #

Dave

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.

Dave United States |

1/15/2008 6:04:35 PM #

Amr

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

Amr Egypt |

1/25/2008 6:41:56 AM #

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.

Vivek India |

Comments are closed

About the author

Amr Elsehemy
MCSD C#.Net,
MCTS Sql 2005,
MCPD Enterprise
avatar
E-mail me Send mail

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar