In WinForm development, the following exceptions often occur when multiple MDI forms are opened and the form is closed in a maximized state:
System.objectdisposedexception: Unable to access the disposed object.
Object name: "Icon".
In System.Drawing.Icon.get_Handle ()
In System.Drawing.Icon.get_Size ()
In System.Drawing.Icon.ToBitmap ()
In System.Windows.Forms.MdiControlStrip.GetTargetWindowIcon ()
In System.Windows.Forms.MdiControlStrip. ctor (IWin32Window target)
In System.Windows.Forms.Form.UpdateMdiControlStrip (Boolean maximized)
In System.Windows.Forms.Form.UpdateToolStrip ()
In System.Windows.Forms.Form.OnMdiChildActivate (EventArgs e)
In System.Windows.Forms.Form.ActivateMdiChildInternal (Form form)
In System.Windows.Forms.Form.WmMdiActivate (message& m)
In System.Windows.Forms.Form.WndProc (message& m)
In System.Windows.Forms.Control.ControlNativeWindow.OnMessage (message& m)
In System.Windows.Forms.Control.ControlNativeWindow.WndProc (message& m)
In System.Windows.Forms.NativeWindow.Callback (IntPtr hWnd, Int32 msg, INTPTR wparam, INTPTR lparam)
This is a bug in the. NET Framework, and the current solution is:
Put here code in your MDI child formclosing Event
Critical code
This. WindowState = Formwindowstate.normal;
Set Formwindowstate.normal, the system automatically sets all subforms to Formwindowstate.normal state and requires the following code to restore
If there are currently only 3 forms (the primary Workbench, the form currently being closed, another form), the other form maximizes
foreach (Form frm in MdiForm.MdiParent.MdiChildren)
{
if (frm. Equals (This) = = False)
{
frm. WindowState = formwindowstate.maximized;
}
}