Home > Software design >  Usercontrol, embedded image as icon in toolbox
Usercontrol, embedded image as icon in toolbox

Time:07-13

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

CodePudding user response:

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.

  • Related