Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VS2008 C# User-controls
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 13-Jul-11 2:03am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Comments
shahzad ch at 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 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
v2
Comments
shahzad ch at 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 at 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.
Shameel at 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".
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 562
1 Sergey Alexandrovich Kryukov 484
2 Maciej Los 325
3 DamithSL 233
4 Mathew Soji 195
0 OriginalGriff 7,168
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,461
3 Manas Bhardwaj 4,876
4 Maciej Los 4,450


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 16 Jul 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100