Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have created usercontrol and i can see in toolbar but my own icon or bitmap image is not displaying for usercontrol. I have already used [ToolboxBitmap(typeof(MyUserControl), "MyIcon.icon")] but still default icon is displayed. I have also set Build Action of image to embedded resource. Regards
Posted

Add your 16*16 Bitmap Image at project root folder. Right click image set Build Action as Embedded Resource.
namespace MyNamespace
{
[ToolboxBitmap(typeof(MyNamespace.MyControlName), "MyNamespace.ImageName.bmp")]
public partial class MyControlName : UserControl
{
}
}
after rebuilding the solution right click Toolbox add your controls .dll file from bin/debug/
Now you will see your control icon with your custom image.

Shahzad ch.
 
Share this answer
 
The namespace of the resource is the project's namespace + the directory structure of the resource file ('\' replace with a '.').

If 'myicon.ico' is in 'images\icons' folder in your project and the project's default namespace (in Project properties) is 'shahzad', then your icon's full namespace would be 'shahzad.images.icons.myicon.ico'.

Also, it is recommended to use bitmaps (.bmp) for toolbox bitmap since .ico does not scale well.
 
Share this answer
 
Comments
shahzad ch 13-Jul-11 10:50am    
Thanks for reply, I have tried this solution but having same issue can you please send me simple example with only single blank usercontrol and a form so that i can check, there may be some linking problem on my side
shahzad ch 14-Jul-11 3:03am    
Dear Shameel, I have separate dll of controls, controls are displayed in toolbox in standard gear icon but my custom icons are not displayed in toolbar. I have a set the build action of bitmap to embeded resource and also added namespace with Myicon [ToolboxBitmap(typeof(MyUserControl), "MyNamespace.MyIcon.bmp")] and after doing this i have rebuild my project many times and even restarted my system and cleaned the cache but problem has not yet resolved. Regards
If you built the assembly in Visual Studio, the default namespace will have been prepended to the resource name. Load the assembly into ILDasm to see what the resource is actually called. By default the default namespace is the same as the project name and it's what your classes will have been placed in if you didn't change them. I.e. if your class files look like

using System.Windows.Forms;

namespace Shahzad.Controls {
 class MyUserControl : UserControl {
  ...
 }
}


... then the resource will probably be called 'Shahzad.Controls.MyIcon.icon'.

EDIT: The ToolboxBitmapAttribute also adds this namespace. So make sure that your class namespace is the same as the default namespace for the project.
 
Share this answer
 
v2
Comments
shahzad ch 13-Jul-11 7:51am    
Dear Permalink,

I have also tried the following but still can't see myicon image in toolbar
MyNamespace
{

[ToolboxBitmap(typeof(MyUserControlName), "MyNamespace.MyIcon.ico")]

public partial class MyUserControlName : UserControl{}
}
Thanks for your reply,

Regards
BobJanova 13-Jul-11 8:03am    
Check what the resource is actually called in ILDasm, and that it is of the appropriate size and format.

According to the documentation the attribute actually looks up the namespace already, I'll update the solution for that.
[no name] 14-Jul-11 3:32am    
In the case of "MyNamespace.MyIcon.ico", your project's default namespace (in project propertie) must be "MyNamespace" and the resource "MyIcon.ico" must reside in your project's root folder (not in a subfolder). Also make sure that in File properties of "MyIcon.ico", Action is set to "Embed Resource".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900