I made a control that inherits directly from ErrorProvider. I thought that applying it the ToolboxBitmap attribute would be enough to get my control to have the same icon on the toolbox as the original control has, but it doesn't. It's strange, as if I add the control to the form, it will appear just as it should, but it doesn't change the toolbox's icon. What am I missing here? I already restarted visual studio and it keeps this behavior.
[ToolboxBitmap(typeof(ErrorProvider))]
public class ErrorProviderEx : ErrorProvider {
...
}
In Visual Studio 2008, the icon specified by ToolBoxBitmap
is not added to the toolbox for any of the components in the current solution for performance reasons. The standard 'gear' icon is used. If you manually add your assembly through the Toolbox...Add Items...dialog, the custom icon will display which is the behavior you are experiencing. Moreover, when you drag ErrorProviderEx to a form, the icon you specified will be used, which again is the behavior you noted in a comment.
Note, in your case, you are using typeof(ErrorProvider)
so you will not have the normal problems of using a custom bitmap.
(This behavior may also been true for Visual Studio 2005. Visual Studio 2003 spoiled us by displaying the icon.) (I personally do not like this new behavior. I am willing to wait an extra second or two for the IDE to retrieve the icon for all controls and components in the solution. I wonder if there is a registry hack to show the icons.)
Is it in the same project? Or a dll you are referencing?
You only get proper icons when referncing a fixed dll. Try building a control dll and referencing it.
So there is a work-around. Supposing your UserControl is in "MyControl" project, add a referrence to itself. That is: Referrence MyControl project in the MyControl project, like you would do in another project. Then the icon will be displayed in the Toolbox of that MyControl project, too.
The easiest way to ensure that a toolbar icon will appear is to create a 16x16 bmp file, place that in your control's project folder, then set it as an embedded resource.
Let's follow a relatively simply example: Suppose you have a component called MyCompany.Components.MyControl. Create a 16x16 bitmap an save it to the root of the MyCompany.Components.MyControl project folder as MyControl.bmp. Then right-click the MyControl.bmp file and select Properties > Build Action > Embedded Resource. At that point, you have one more step: add the ToolboxBitmap attribute above your class definition (if using partial classes, I place the attribute in the control's main .cs file--not in the Designer.cs file). To do that, use this syntax:
[ToolboxBitmap(typeof(MyControl), "MyControl.bmp")]
Be absolutely sure that:
- The namespace for your MyControl project class matches the folder structure for where your project appears in the solution.
- Might seem obvious, but be sure you have cited the correct name for your bmp file.
Your bitmap should appear whenever your control is added to a toolbox, whether it's manually added or programmatically added.