微软bug解决方法:System.ObjectDisposedException: 无法访问已释放的对象。对象名:“Icon”

在WINFORM开发中,对当打开多个MDI窗体,并且在最大化状态下关闭窗体时,常常出现以下异常:

System.ObjectDisposedException: 无法访问已释放的对象。
对象名:“Icon”。
   在 System.Drawing.Icon.get_Handle()
   在 System.Drawing.Icon.get_Size()
   在 System.Drawing.Icon.ToBitmap()
   在 System.Windows.Forms.MdiControlStrip.GetTargetWindowIcon()
   在 System.Windows.Forms.MdiControlStrip..ctor(IWin32Window target)
   在 System.Windows.Forms.Form.UpdateMdiControlStrip(Boolean maximized)
   在 System.Windows.Forms.Form.UpdateToolStrip()
   在 System.Windows.Forms.Form.OnMdiChildActivate(EventArgs e)
   在 System.Windows.Forms.Form.ActivateMdiChildInternal(Form form)
   在 System.Windows.Forms.Form.WmMdiActivate(Message& m)
   在 System.Windows.Forms.Form.WndProc(Message& m)
   在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

这是.net框架的BUG,目前的解决方法是:

Put this code in ur MDI Child FormClosing Event

//关键代码

this.WindowState = FormWindowState.Normal; 

//设置FormWindowState.Normal,系统自动将所有子窗体设置为FormWindowState.Normal状态,需要下面的代码进行还原

            //如果当前只有3个窗体(主工作台,当前正在关闭的窗体,另外一个窗体),则另外一个窗体最大化
                foreach (Form frm in mdiForm.MdiParent.MdiChildren)
                {
                    if (frm.Equals(this) == false)
                    {
                        frm.WindowState = FormWindowState.Maximized;
                    }
                }

posted on 2014-07-18 16:09  IOTer  阅读(9365)  评论(0编辑  收藏  举报

导航