I am trying to change existing icons on a toolbar. The icons are placed under "img" directory. When I replace an old icon with a new one and build\start my project, I don't see the new icon. Can somebody let me know what am I doing wrong ?

Thanks a lot!

Dani AI

Generated

Short summary and what to check next: is right that the toolbar ultimately shows whatever images are stored in the form/ImageList resource rather than the raw file on disk, so simply overwriting a file can leave the designer/runtime using the old, embedded copy. Since cannot see icons in the designer, the images are likely serialized into a .resx or the ImageList was created/filled at design-time or compiled into the assembly — the goal is to discover which resource actually contains the images and then update that resource.

Practical inspection steps (fast): open the form's Designer file (FormName.Designer.cs) and look for where the ImageList is referenced — that will point to the .resx key or to a Properties.Resources property. If the ImageList is created only at runtime, design view will not show icons; if the designer created it you will see an ImageStream or resources access there. If the regular resource editor shows “No resources to load,” open the .resx in a text editor and search for <data name="..."> entries or search the whole solution for the icon base name (for example "connect"). That will locate the resource key you need to update.

If moving to project resources is acceptable, this pattern is reliable (convert Icon to Bitmap if needed and set ImageList properties before adding images):

var ico = Properties.Resources.connect as Icon; 
Image img = ico != null ? ico.ToBitmap() : (Image)Properties.Resources.connect;
var list = new ImageList { ImageSize = new Size(16,16), ColorDepth = ColorDepth.Depth32Bit };
list.Images.Add("connect", img);
toolBar.ImageList = list;
toolBar.Invalidate();

Extra tips: after changing a resource always Clean + Rebuild (delete bin/obj if needed), remove and re-add the image in the designer if the filename did not change, and confirm you are editing the correct .resx (form-level vs Project->Resources). If you still cannot find the embedded item, inspect the compiled EXE with a resource viewer (or a tool like ILSpy) to list manifest resources and match the full resource name.

Recommended Answers

All 3 Replies

I am trying to change existing icons on a toolbar. ... When I replace an old icon with a new one and build\start my project, I don't see the new icon.

Hi HLD77,

I had this problem for a long time before I discovered what was going on. The Toolbar icons are all loaded in an ImageList control. The ImageList control is associated with the Toolbar to provide the various icons for your buttons.

The ImageList loads the icons by making a copy, (as a resource I believe, in your project) the first time you add the icon to the ImageList. The image on disk then no longer matters to the ImageList. To update the Toolbar, you must update the ImageList by deleting the old image and uploading the new one. When you set focus back to the Toolbar you should see the new icon on the toolbar. When re-loading the icon, you may need to move it to the right index position. There may also be some issues if your filename is exactly the same. If this happens, delete the icon, click on the design surface to update the toolbar, then re-upload the new icon. Should work.

It may seem annoying to have to update your icons all the time, but once you understand it, it's no big deal and it's nice not to have to bother with manually managing the icon resource yourself.

Cheers,
Steve

Thanks Steve. Unfortunately I don't see icons in the toolbar in design view and hence I cannot update\delete the icons. :sad:

I am making changes to existing code and it's kind of hard to figure out which .resx file has these icon information. When I tried to edit the .resx file that is most likely to have this information in Resurce editor, I received "No resources to load in this file" error. Below is the code in case someone can figure out what's going on. Thanks!

    try
    {
            this.ExecutingAssembly = System.Reflection.Assembly.GetExecutingAssembly();
            this.FormResources = new System.Resources.ResourceManager( "MyApp.MyAppForm", this.ExecutingAssembly );

            Icon ic1 = (System.Drawing.Icon) this.FormResources.GetObject ("connect.ico"); 
            Icon ic2 = (System.Drawing.Icon) this.FormResources.GetObject ("disconnect.ico"); 

            ImageList imglist = new ImageList();
            imglist.Images.Add(ic1);
            imglist.Images.Add(ic2);

            this.toolBar.ImageList = imglist;
    }


catch(Exception ex)
{
MessageBox.Show (ex.Message + ex.StackTrace);
}

for (int iToolButt = 0; iToolButt < toolbarText.Length; iToolButt ++)
{
    ToolBarButton tButt = new ToolBarButton();
    tButt.ImageIndex = iToolButt;       
    this.toolBar.Buttons.Add(tButt);
}

Well, looks like it's time to go into full on debug mode. :eek:

First you'll need to determine whether the resources are actually getting loaded. Add some code to print out all the names in the resource manifest. The following should get you started. Just loop over and print.

string[] resources = Assembly.GetExecutingAssembly().GetManifestResourceNames();

If you see the icon names, then you just have to figure out how to access the resource. The following might help:

strm = Assembly.GetExecutingAssembly().GetManifestResourceStream(fullResourceName);
icon = new Icon(strm);

Also, Ensure that your icons are being loaded as application resources. Right-click the icon file in Solution Explorer, select properties, and then specify the Build Action as embedded resource. If you don't, the icon will not be an application resource and cannot be accessed the way your code is doing it.

Cheers,
Steve

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.