locked
Specifying [System.Drawing.ToolboxBitmap(@"somepath")] on my control class has no effect RRS feed

  • Question

  • This is the line I used.  What did I do wrong?

    [System.Drawing.ToolboxBitmap(@".\Properties\Resources\FlatBtn.icon.png")]
    public partial class FlatBtn : System.Windows.Forms.Button
    
    


    Will Pittenger
    Sunday, June 5, 2011 7:21 PM

Answers

  • None of that had any effect.  In fact, when I added controls from a release build of ObjectListView or my CustomControls DLL to the toolbox, I still got default icons.
    Will Pittenger

    That's because the path is not correct, and unfortunately I don't know of a way to debug it :( .

    If I recall correctly the path is relative to the location of the compiled assembly, so try setting the value "Copy to output directory" for the bitmap to "Copy if newer".

     

    I personally use the alternative, embedding the images as resources and specifying the type and the bitmap resource name, thus avoiding the situation where I have a bunch of images and need to deploy them alongside the assembly :

    1. Put the bitmap in the same folder as the control file.

    2. Change "Build Action" for the bitmap file to "Embedded Resource"

    3. The control should be is in the project default namespace.

    4. Add the attribute:

     

    namespace CompanyName.SolutionName.ProjectName // this is the project default namespace
    {
    	[ToolboxBitmap(typeof(ToolStripCheckBox), "ToolStripCheckBox.bmp")]
    	public class ToolStripCheckBox : System.Windows.Forms.ToolStripControlHost
    	{
    		// ...
    	}
    }
    

     

    This way the type is used to locate the appropriate embedded image (resource) in the compiled assembly. The type's namespace will also be used to locate the resource, so if this organization does not suit your needs, you may need to play around for a while. I suggest you start from this, make sure it works, and then move on.

    So in the example above, the ToolboxBitmap attribute specifies the resource named CompanyName.SolutionName.ProjectName.ToolStripCheckBox.bmp to be used as the toolbox bitmap for this control.

    If you don't make it on your own, post how you organized your files and namespaces to make it easer to test.

    Best regards,
    Vladimir 


    • Proposed as answer by Helen Zhou Monday, June 13, 2011 5:23 AM
    • Marked as answer by Helen Zhou Wednesday, June 15, 2011 1:45 AM
    Tuesday, June 7, 2011 3:33 PM

All replies

  • The bitmap doesn't show when using the solution that contains the control.  If you load the control in a separate solution, though, the bitmap should show.
    MCP
    Sunday, June 5, 2011 7:43 PM
  • Well, that makes testing difficult.  I would like to ensure it looks right.
    Will Pittenger
    Sunday, June 5, 2011 7:49 PM
  • It is not difficult to test at all.  Just compile your control DLL, then create a new solution and add the DLL (not the solution, the compiled DLL) to the new project and see if it shows properly in the toolbox.
    MCP
    Sunday, June 5, 2011 7:54 PM
  • Why doesn't it work? One of the ways the ObjectListView author tells you that you can include it in your solution is to add it's project file.  Doesn't work if you have VC++ Express or VB Express.  But otherwise, it works--until you notice how much of the toolbox's vertical space ObjectListView consumes.  It sure is nice being able to step into ObjectListView code though.  Kind of leaves me torn which way to include it in my project.

    Will Pittenger
    Sunday, June 5, 2011 7:57 PM
  • Hi Will,

    What you need is to Rebuild the project. You can refer to this MSDN document How to: Provide a Toolbox Bitmap for a Control for more information. 

    "The bitmap does not appear in the Toolbox for autogenerated controls and components. To see the bitmap, reload the control by using the Choose Toolbox Items dialog box. For more information, see Walkthrough: Automatically Populating the Toolbox with Custom Components"


    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, June 7, 2011 8:17 AM
  • None of that had any effect.  In fact, when I added controls from a release build of ObjectListView or my CustomControls DLL to the toolbox, I still got default icons.
    Will Pittenger
    Tuesday, June 7, 2011 8:38 AM
  • None of that had any effect.  In fact, when I added controls from a release build of ObjectListView or my CustomControls DLL to the toolbox, I still got default icons.
    Will Pittenger

    That's because the path is not correct, and unfortunately I don't know of a way to debug it :( .

    If I recall correctly the path is relative to the location of the compiled assembly, so try setting the value "Copy to output directory" for the bitmap to "Copy if newer".

     

    I personally use the alternative, embedding the images as resources and specifying the type and the bitmap resource name, thus avoiding the situation where I have a bunch of images and need to deploy them alongside the assembly :

    1. Put the bitmap in the same folder as the control file.

    2. Change "Build Action" for the bitmap file to "Embedded Resource"

    3. The control should be is in the project default namespace.

    4. Add the attribute:

     

    namespace CompanyName.SolutionName.ProjectName // this is the project default namespace
    {
    	[ToolboxBitmap(typeof(ToolStripCheckBox), "ToolStripCheckBox.bmp")]
    	public class ToolStripCheckBox : System.Windows.Forms.ToolStripControlHost
    	{
    		// ...
    	}
    }
    

     

    This way the type is used to locate the appropriate embedded image (resource) in the compiled assembly. The type's namespace will also be used to locate the resource, so if this organization does not suit your needs, you may need to play around for a while. I suggest you start from this, make sure it works, and then move on.

    So in the example above, the ToolboxBitmap attribute specifies the resource named CompanyName.SolutionName.ProjectName.ToolStripCheckBox.bmp to be used as the toolbox bitmap for this control.

    If you don't make it on your own, post how you organized your files and namespaces to make it easer to test.

    Best regards,
    Vladimir 


    • Proposed as answer by Helen Zhou Monday, June 13, 2011 5:23 AM
    • Marked as answer by Helen Zhou Wednesday, June 15, 2011 1:45 AM
    Tuesday, June 7, 2011 3:33 PM