16

I have a .NET 3.5 MDI WinForms application.

I set a a child form's Icon property, and the icon shows up correctly in the top left corner of the form. I then maximize the child form and the icon is still OK.

With the child form still maximized, I open another child window, which automatically comes up maximized. This form's icon is not the one in the Icon property, but the default .NET icon (the one with the blue, red, and yellow squares). However, if I resize the MDI parent form, the icon resets itself and displays properly.

Does anyone have a workaround or know why this happens?

lc.
  • 113,939
  • 20
  • 158
  • 187

9 Answers9

12

A slight modification to Calanus' solution:

    private void MdiBase_Load(object sender, EventArgs e)
    {
        // Fixes bug where loading form maximised in MDI window shows incorrect icon.
        this.Icon = Icon.Clone() as Icon;
    }

This allows you to set the icon at design time (just as you would for other forms), and does not need any hard-coded file or resource accessing.

Tom
  • 326
  • 3
  • 8
  • For me, this worked when adding the code in the MDI child's form; whereas the example suggests putting the code in the MDI parent's form. – etri Feb 17 '22 at 16:29
9

Right I have found a solution...

The workaround for this is to set the icon again on the load event of the child form as follows:

private void StatsForm_Load(object sender, EventArgs e)
{
    //bug that means you have to set the desired icon again otherwise it reverts to default when child form is maximised
    Icon = new System.Drawing.Icon("research.ico");
}

This does mean that you will have to first add the icon file in question into your VS project/solution and set it to "Copy Always" so that is copied when your solution is built.

HTH Calanus

Calanus
  • 25,619
  • 25
  • 85
  • 120
  • 2
    Works like a charm. Thanks. For whatever reason I never thought to try resetting the icon. It also works as an embedded resource, so you don't have a ton of (replaceable) files sitting in the final directory. – lc. Jun 30 '09 at 00:33
4

I found that the only solution was to deactivate and then reactivate the MDI child:

document.Show();
// Work-around for error in WinForms that causes MDI children to be loaded with the default .NET icon when opened maximised.
ActivateMdiChild(null);
ActivateMdiChild(document);

This is the solution given in this reply on MSDN forums and it worked for me.

open-collar
  • 1,404
  • 1
  • 16
  • 22
  • It works, but creates a problem if you use ShowDialog anywhere. – Drake Sep 30 '10 at 13:40
  • If you use ShowDialog instead of Show in your code, execution blocks on that call. Only once the dialog is closed do the calls to ActivateMdiChild run, but the second one won't work because `document` no longer exists at that point – alldayremix Jul 12 '13 at 16:41
3
private void frmChild_Shown(object sender, EventArgs e)
{
    // Work-around for maximized BUG
    this.Icon = this.MdiParent.Icon;
    this.WindowState = FormWindowState.Normal;
    this.WindowState = FormWindowState.Maximized;
}
Christoffer Lette
  • 14,346
  • 7
  • 50
  • 58
RickNL
  • 31
  • 1
2

Adding this as the first line in the Form_Load method on the MDI Children works for me:

this.Icon = new Icon(this.Icon, this.Icon.Size);
Dave Lucre
  • 1,105
  • 1
  • 14
  • 16
2

I found out this will fix the problem as well.

myForm.WindowState = FormWindowState.Normal;
myForm.Show();
myForm.WindowState = FormWindowState.Maximized;
VisserP
  • 21
  • 1
1

My solution: Leave the MdiChild "ShowIcon" property set to true, assign a 1x1 transparent icon. Problem solved.

Jose Wales
  • 11
  • 1
  • 1
    Maybe I'm missing something, but I'm not sure how this applies to my original question. The problem is not that an icon shows when I don't want one, but rather that the *icon I want to show* doesn't show and is instead replaced by the default. – lc. May 13 '10 at 05:55
1
form.WindowState = FormWindowState.Normal
form.Show()
form.WindowState = FormWindowState.Maximized
form.Show()

Solved my problem!

Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
jesus
  • 11
  • 1
0

The best workaround that I found to fix this issue is here.

aNewForm.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedDialog
AddHandler aNewForm.SizeChanged, AddressOf Form_SizeChanged

aNewForm.Show()
aNewForm.WindowState = FormWindowState.Maximized

RemoveHandler aNewForm.SizeChanged, AddressOf Form_SizeChanged

the handler

Protected Sub Form_SizeChanged(ByVal sender As Object, ByVal e As EventArgs)

    If WindowState = FormWindowState.Maximized Then
        If FormBorderStyle <> FormBorderStyle.Sizable Then FormBorderStyle = FormBorderStyle.Sizable
    End If

End Sub
Drake
  • 8,225
  • 15
  • 71
  • 104