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:
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.