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

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值