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!

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);
}

Edited 3 Years Ago by pyTony: fixed formating

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

This article has been dead for over six months. Start a new discussion instead.