微軟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;
                    }
                }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM