2

I'm making a control for Visual Studio, and I want to give it an icon to see in the toolbox.

I can easily make it happen when pointing at a location on the hard disk, like this:

[ToolboxBitmap("G:\Graphics\icon.png")]

But I don't want to depend on some folder, I want to embed 'icon.png' as a resource and use it from there.

I have the image embedded, and it works when using it in the control i.e. as an image in a picturebox, but does not when it comes to the ToolBoxBitmap:

Embedded image in Visual Studio

I have run an ildasm.exe on the dll-file, and the manifest adresses the image like this:

Manifest from ildasm.exe

I have tried to use the plain address from the manifest (as suggested on other websites and questions about this):

[ToolboxBitmap("DemoIconInToolBox.icon.png")]

But still to no avail.

I still only get the standard-icon in the toolbox (This is an other form, made to test the control):

Icon in toolbox not showing

Can anyone tell me what I am doing wrong?

This is taking place on a Win10 Pro, Visual Studio 2022, C# Forms.

Sincerely / Best regards

Mads Aggerholm
  • 432
  • 1
  • 5
  • 13
  • 1
    1) Select the project and add the image. 2) Select the image and in the properties window, set the `BuildAction` to `EmbeddedResource`. 3) add the attribute `[ToolboxBitmap(typeof(DemoIconInToolBox), "icon.png")]`. 4) Rebuild. 5) In the target project, remove the library. 6) Add it again. I'm not sure about the `png` format. Used to be `bmp` format. – dr.null Jul 10 '22 at 08:05
  • Note, `DemoIconInToolBox.icon.png` is translated to the `icon.png` is in a folder named `DemoIconInToolBox` under the project's tree node. So and from the screenshot, it's obviously the wrong path. – dr.null Jul 10 '22 at 08:10
  • 1
    dr.null, thank you for your input, but as for your first comment, it say 'DemoIconInToolBox' is a namespace but is used as a type. As for your second comment, I have already tried [ToolboxBitmap("DemoIconInToolBox.icon.png")], but that doesn't work either. I'll try to change it to a bmp-file to see if that could be the cause. – Mads Aggerholm Jul 10 '22 at 15:22
  • 1
    You are welcome. I though `DemoIconInToolBox` is the user control. Hard to see that in the image without zooming. – dr.null Jul 10 '22 at 16:46

1 Answers1

3

I own a sincere "thank you" to dr.null, for bringing me on the right track.

[ToolboxBitmap(typeof(DemoIconInToolBox), "icon.png")] didn't work, but when I changed 'DemoIconInToolBox' to 'UserControl1' it worked: [ToolboxBitmap(typeof(UserControl1), "icon.png")]

It looks like it was placing the icon-file in the resources-file that was the bad move.

Adding it as an embedded resource directly under the project was the key.

This also takes care of my next question, which would have been: "In a control with severel different controls in it, how do you give each of them in icon?"

And the answer to that question is: "You just add the icon to the project, and repeat the ToolBoxBitMap-line with the typeof(<controlname>) just above the declaration of every control."

Thank you dr.null.

Mads Aggerholm
  • 432
  • 1
  • 5
  • 13