简体   繁体   中英

Application.OpenForms empty after minimizing from taskbar

I have a MDI parent containing many maximized MDI children.

I prevent the MDI parent from closing with this code:

private void OnMainFormFormClosing(...)
{
    if (e.CloseReason == CloseReason.UserClosing)
    {
        e.Cancel = true;
        // Minimize to taskbar
        this.WindowState = FormWindowState.Minimized;
    }
}

Then I maximize it from the taskbar with this code:

private void OnOpenToolStripMenuItemClick(...)
{
    // Maximize it from taskbar
    this.WindowState = FormWindowState.Maximized;
}

Problem - After I restore the MDI form from the taskbar, the Application.OpenForms collection is empty. How could I repopulate it? Many thanks.

That is not the code that caused the problem. The best way to find it is by pasting this code into your form class:

    protected override void OnHandleCreated(EventArgs e) {
        base.OnHandleCreated(e);
    }

And set a breakpoint on it. It will hit the first time the form's window gets created. When it hits again, look at the debugger's Call Stack window. You'll see the property assignment that caused the native window to be recreated.

This is otherwise an unsolved Winforms bug, some properties are difficult since they are style flags in the native CreateWindowEx() winapi function. Changing the style flag requires the native window to be recreated. Examples of such properties are Opacity and ShowInTaskbar. That works fairly well, but the Application class loses track of the form instance when the window gets destroyed. Workarounds are to avoid changing that property or to stop relying on Application.OpenForms.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM