locked
ToolboxBitmap problem RRS feed

  • Question

  • Does anyone know why the following does not leave me with a Custom ToolboxBitmap.  I have included a 16 x 16 Bitmap called C_Button as an Embedded Resource in the Library.  I have a set of controls in the library all with their own Custom bitmaps, but I just get the default Gear bitmap.  Is this a restriction because it is an Express Version?

    All comments gratefully received.

    namespace CcNet2005
    {
         [ToolboxBitmap(typeof(C_Button))]
        public partial class C_Button :  System.Windows.Forms.Button
        {
           // Some Code
        }

    }
    Monday, April 10, 2006 11:55 PM

Answers

  • There are a lot of ways it won't work, I've found only one reliable way, documented by this webpage.  The step-by-step instructions: create a new class library project, name it ClassLibrary1.  Add a bitmap resource and name it "MyButton.bmp".  Click the bitmap in the Solution Explorer and change the Build Action to "Embedded Resource".

    Write your control class code like this:
    using System;
    using System.Drawing;
    using System.Windows.Forms;

    internal class resfinder { }

    namespace MyControls
    {
      [ToolboxBitmap(typeof(resfinder), "ClassLibrary1.MyButton.bmp")]
      public class MyButton : Button
      {
      }
    }

    The "resfinder" class is a trick to let the GetImageFromResource() method find the bitmap resource in the proper namespace.  This is necessary because I made the namespace for the control ("MyControls") different from the default namespace of the class library ("ClassLibrary1").  The resfinder type reference forces .NET to look for the bitmap resource in "ClassLibrary1.MyButton.bmp" rather than "MyControls.ClassLibrary1.MyButton.bmp".

    Build the project.  Open the project that will use your control.  Right-click the tool box and select "Choose items...".  Click on Browse and select the ClassLibrary1.dll assembly.  You should now see the MyButton control with the proper bitmap.
    Saturday, July 29, 2006 5:43 PM
    Moderator

All replies

  • Try another overloaded constructor where you can specify name of your embedded icon resource file:

    [ToolboxBitmap(typeof(C_Button), "your_button.bmp")]

     

    Tuesday, April 11, 2006 1:28 AM
  • Thanks for the suggestion.  Unfortunately, it doesn't work (for me anyway). 
    I have tried your suggestion on controls that are based on existing windows form controls as well as those that are based on the UserControl Class.  No luck. 
    Am I missing something fundamental? 
    Could there be something else that I need to do beyond using the correct constructor?

    FYI: 
    Using Visual C# 2005 Express Edition to create the Control Library. 
    Have tested the control libary and all controls work as expected other than the problem as described. 
    Have tried the Control Library in a Windows Application using both Visual C# 2005 Express and Visual Basic 2005 Express Edition.  Same Results in both cases.
    Tuesday, April 11, 2006 11:47 AM
  • Heres an interesting thing, if I have a look at the compiled assembly (Called CcNet2005.dll) using a Resource Hacker, the bitmaps I am trying to use for the ToolboxBitmap's are not present.  I have tried various configurations for the build but they never appear.  The only resources that appear are the 'Application' Icon and the Version Info.

    Is this normal for .NET assemblies?  Or am I missing something fundametal?
    Tuesday, April 11, 2006 3:47 PM
  • This problem is really bugging me!

    Anyone know how I can get Microsoft to answer the question?

    Is there anyone from Microsoft checking these threads?



    Friday, April 21, 2006 11:47 PM
  • Hi StoneTheCrows_,

    Could it be that you are attempting all this from within the same solution?

    If so, please note that control attributes behave differently at "authoring time" compared to "run-time". Ask no further. After having spent several days on this matter, I still don't understand why this should be a problem.

    Try starting a brand new solution, containing a new windows application.
    Import your library into the toolbox by:
    > right-clicking on the toolbox,
    > selecting "choose Items.." from the popup menu,
    > select the ".NET Frameworks Components" tab,
    > and browse to your assembly.

    FYI
    I tried this on Visual Studio 2005 pro. There may be differences using the Express edition.

    Good luck,
    Tonn

    Wednesday, April 26, 2006 11:20 AM
  • Hi StoneTheCrows_,

    i'm having exactly the same problem. I'm using VS2005PRO and I can't get a custom icon in the toolbox... I'm searching all over the internet, but can't find a solution... If you find a solution, please provide it to the forum. I think more people are having this problem....
    Monday, May 1, 2006 9:28 PM
  • Victim of the same problem, i have been trying to get the bitmap since 2 days, tried all possible ways,  no use..

    I think some body from microsoft should answer this question.

    Thanks

    Tuesday, June 27, 2006 2:21 PM
  • Just wanted to add to the list of people who this isn't working for.  I've tried every way I can possibly find to no avail.  Help? Please?
    Friday, July 28, 2006 6:17 PM
  • OK,

    I read the entry from Tonn again closer and figured this out to some small degree.  If I do what he said and choose items and do a browse to my actual .dll I get my controls importing in and they have correct icons.  I think this is a problem with visual studio, but this is at least a work around.
    Friday, July 28, 2006 7:48 PM
  • There are a lot of ways it won't work, I've found only one reliable way, documented by this webpage.  The step-by-step instructions: create a new class library project, name it ClassLibrary1.  Add a bitmap resource and name it "MyButton.bmp".  Click the bitmap in the Solution Explorer and change the Build Action to "Embedded Resource".

    Write your control class code like this:
    using System;
    using System.Drawing;
    using System.Windows.Forms;

    internal class resfinder { }

    namespace MyControls
    {
      [ToolboxBitmap(typeof(resfinder), "ClassLibrary1.MyButton.bmp")]
      public class MyButton : Button
      {
      }
    }

    The "resfinder" class is a trick to let the GetImageFromResource() method find the bitmap resource in the proper namespace.  This is necessary because I made the namespace for the control ("MyControls") different from the default namespace of the class library ("ClassLibrary1").  The resfinder type reference forces .NET to look for the bitmap resource in "ClassLibrary1.MyButton.bmp" rather than "MyControls.ClassLibrary1.MyButton.bmp".

    Build the project.  Open the project that will use your control.  Right-click the tool box and select "Choose items...".  Click on Browse and select the ClassLibrary1.dll assembly.  You should now see the MyButton control with the proper bitmap.
    Saturday, July 29, 2006 5:43 PM
    Moderator
  • This worked great for me too.  Some notes:

    1. Add internal class resfinder { } once - in one of your classes.

    2. ClassLibrary1 is the Default namespace that appears in your Project->Properties property sheet.  Use Reflect to double check the full names of the bitmaps in the resource file.  If there are no resources, see #3 below.

    3. Don't forget to change the Build Action to Embed Resource in the Properties for each bitmap file.

    Tuesday, November 7, 2006 5:59 AM
  • Visual Basic:

    I have project with assembly name Tools, Root namespace Tools. All embeded resources (thos bitmaps) are stored in the ResourcesFolder. My control is in namespace declared Namespace Windows.Forms, so effective namespace is Tools.Windows.Forms. Class is named TransparentLabel. The only way that worked for me was: Bitmap name is Windows.Forms.TransparentLabel.bmp. Attribute is applied

    Code Snippet

    Namespace Windows.Forms

    <ToolboxBitmap(GetType(TransparentLabel), "TransparentLabel.bmp")> _

    Public Class TransparentLabel : Inherits Label

     

     

    Sunday, May 13, 2007 10:33 PM
  • I tried all the ways to have an Icon for my control but I never get it work on VB 6.0. always its an empty icon

     

    Wednesday, May 23, 2007 3:30 PM
  • I fixed it by the following code, now the icon appears on VB6

    [ComRegisterFunction]

    public static void ComRegister(Type t)

    {

    using (RegistryKey subkey = key.CreateSubKey("DefaultIcon"))

    {

    string path = Assembly.GetAssembly(typeof(MyControl.Class1)).Location;

    subkey.SetValue("", path + ",0");

    }

    using (RegistryKey subkey = key.CreateSubKey("ToolBoxBitmap32"))

    {

    string path = Assembly.GetAssembly(typeof(MyControl.Class1)).Location;

    subkey.SetValue("", path + ",1");

    }

    .

    .

    .

    }

     

    Wednesday, May 23, 2007 7:11 PM
  • In my component library there were a lot of images so I gathered them into "resources" subfolder. When specifying the bitmap I had to add the "resources." prefix:
    [ToolboxBitmap(typeof(GuiFrame), "Resources.GuiFrameIcon.bmp")]

    Thursday, July 19, 2007 11:15 AM
  • What solved all my problems was to change a default namespace in project properties to real one, assigned by me after project creation.
    Since then, no problems with bitmaps.
    hth
    • Proposed as answer by consolitech Thursday, May 14, 2009 9:47 PM
    Monday, September 24, 2007 5:36 PM
  • Also, make sure that you  include the bitmap in the solution and embed. Not in the resources.
    Thursday, May 14, 2009 9:51 PM
  • I managed to get this to work only by using .ico files instead of .bmp.  My icon file has two 16x16 images, one 4-bit and the other 24-bit; Visual Studio's toolbox selects the 24-bit one.

    Also note that the the icon only works when used from a different solution file.

    namespace CodeSelector
    {
      [ToolboxBitmap(typeof(CodeSelector.Selector), "Images.Icon.ico")]
      public class Selector : ComboBox
    }
    The icon is in a solution directory called "Images".
    Friday, June 12, 2009 3:40 PM
  • When the bitmap is an embedded resource make sure to have the second parameter as the complete path to the bmp file.
    Means, if the namespace is "My.Controls" and the bitmap is under a folder path "NewControl\Images" and the bitmap name is "Icon.bmp", then the path will be My.Controls.NewControl.Images.Icon.bmp.

    So the attribute should look like
    [ToolboxBitmap(typeof(NewControlResFinder), "My.Controls.NewControl.Images.Icon.bmp")]

    NewControlResFinder class should be defined outside the namespace. Mark it internal so that no one else can use it.

    Hope this will help you all guys.
    • Edited by Shaji Padavil Friday, July 3, 2009 2:43 AM Missed out some explanation
    Friday, July 3, 2009 2:23 AM
  • There is only one way I have know this to work. I read about it some time ago when I was having the same problem in VB.net. Basically, first you have to rename your Bitmap resource to match the namespace of the control. For example, if I take your code:

    namespace CcNet2005
    {
         [ToolboxBitmap(typeof(C_Button))]
        public partial class C_Button :  System.Windows.Forms.Button
        {
           // Some Code
        }
    
    }

    The way it would work is that the bitmap (C_Button.bmp) would have to be renamed to "CcNet2005.C_Button.bmp". When you have renamed the image file, right click the same folder that your control is in, in the Solution Explorer and click "Add -> Existing Item..." Select "All Files" from the type drop down list and select your image file (CcNet2005.C_Button.bmp). Once you have added it to the same folder as your control, make sure under "Properties" it is marked as an Embedded Resource. Once you have done that it should all work fine. Just to clarify, you do not have to modify the code you have posted, as this is correct.
    • Proposed as answer by Ant2100 Sunday, December 20, 2009 4:11 PM
    • Edited by Ant2100 Sunday, December 20, 2009 4:36 PM Typing error
    Sunday, December 20, 2009 4:11 PM
  • Tried a lot, and this is what worked for me.

    The File is mcbl3006s.ico (16x16, 16colors), build action set to embedded resorce. It was necessary for me to name it .ico, not .bmp.

     

    namespace MCBL3006S

    {

     

    [ToolboxBitmap(typeof(mcbl3006s), "MCBL3006S.mcbl3006s.ico")]

     

    public class mcbl3006s:SerialPort......

     

     

     

    Wednesday, April 14, 2010 9:35 AM
  • Hi folks.

    Here is an explanation of an approach that worked for me.

    I hope this will help someone

     

    let:
    + {NAMESPACE} to be the (CASE SENSITIVE!!!) project's namespace of the project that exposes the customcontrol
    The project is a class library
    in this case {NAMESPACE}="MYFIRM.Web.UI"
    Project's namespace can be changed in "Application" tab in project properties ("Assembly name" and  "default namespace" parameters)

    + {CLASSNAME} to be the CustomControl class
    in this case {CLASSNAME}="MYSampleControl"

    + {PICTURE} to be the filename of the icon picture (embedded as resource)
    in this case {PICTURE}="MYSampleControl.bmp"
                 
    ALL this conditions must be satisfied in order to get the whole thing working:
    - Picture file must be in the same project folder as the .cs (of .vb) CustomControl class file
      (the class will be used as the class referenced to resolve picture's namespace)
    - Both class file (.cs or .vb) and picture file (.bmp) are in the root folder of the project
      (if the picture is in a subfolder, the subfolder name will be a part of the picture resource's namespace
       and this will make it more complicated)
    - CustomControl class belongs to {NAMESPACE} namespace
      so both class (due to the "namespace {NAMESPACE}" that contains its declaration) and picture resource
      (if it is put in project's root folder it belongs to project's namespace "{NAMESPACE}") will have tha same namespace
    - ToolBoxBitmap attribute is used with these parameteres
       typeof({CLASSNAME}) and  "{NAMESPACE}.{PICTURE}"
    - The picture is a 256 color 16x16 bitmap, and is marked as Embedded Resource (as build condition)


    let this be the sample code


    namespace MYFIRM.Web.UI
    {
        [System.Drawing.ToolboxBitmap(typeof(MYSampleControl), "MYFIRM.Web.UI.MYSampleControl.bmp")]
        public class MYSampleControl : WebControl
        {
          
        }
        
    }

     

    Bye

    gdc

     

     

    Friday, November 5, 2010 9:14 AM
  • Days of searching - this is the only solution that worked.  Thank you 100x's!
    Tuesday, February 15, 2011 7:00 PM