4
votes

I have a Windows Forms application that uses a custom form to display a progress bar during a long operation. This form is currently set up to display on the taskbar along with the other windows in my program. However, while the other windows of my application show the appropriate application icon, this custom form displays only the default icon:

Default application icon in the taskbar

I set the icon in the designer to the same icon file all of my other windows use, but only this particular form has this problem. The icon shows up correctly in the form's caption as well. Manually setting the icon with form.Icon = Resources.Sample_Icon; in the either the child form's constructor or in the using loop seems to have no effect.

Here's a code sample that reproduces the problem:

// Inside Form1.cs on a button click
using (var form = new SampleForm())
{
    form.Show();

    for (int i = 0; i < numberOfIterations; ++i)
    {
        // Do work here in a real program

        // This function just calls progressbar.PerformStep():
        form.UpdateProgress();
    }
}

Changing the Show() to ShowDialog() does show the correct icon, but a DialogResult doesn't really have meaning in this situation.

I suspect that there is some event or message which sets the Windows taskbar icon which never gets processed, but when I tossed in a form.Refresh() and an Application.DoEvents() (just to see if it made a difference), I still had the default icon.

What am I missing? And, why does ShowDialog work properly?

I'm using C# 4.0.

1
I thought this might be a distraction from the question, but interestingly enough, instantiating a Microsoft.Office.Interop.Excel.Application in the using loop is enough to get the form to show its icon, but reading files from disk is not.Troyen

1 Answers

1
votes

Forms don't inherit their icon from the parent form, even if Show or ShowDialog is called. You should probably try changing the other form's icon explicitly. Either do it in the current code:

form.Icon = <your icon>;

Or, do it in the loading code of your other form, just refer to the form itself.