关闭MDI子项将关闭父窗体 [英] Closing MDI child closes the parent form

查看:83
本文介绍了关闭MDI子项将关闭父窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个新的MDI父表单。我已将该表单的MDI容器设置为True。我创建了一个菜单条。在单击菜单条时,必须显示MDI子项。只有条件成立时才必须显示MDI Child。所以我在MDI Child - Form Load事件处理程序中添加了这段代码,这样,



I've created a new MDI parent form. I've set MDI Container of that form to True. I've created a menu strip. While clicking on the menu strip an MDI child has to be shown. The MDI Child has to be shown only if a condition is true. So I added the piece of code in the MDI Child - Form Load event handler like this,

private void Form_Load(...)
{
...
  if(condition == false)
  {
     this.Close();
  }

}





但遗憾的是,整个申请已经关闭,而不是仅仅是儿童表格。请问有什么解决方案吗?



我尝试了什么:



我'我试过使用this.Dispose()。这也行不通。如果我使用this.Close()没有生成异常,使用this.Dispose()时会抛出一个异常,说:无法显示Disposed对象。



有什么想法吗?



But unfortunately, the entire application closes instead of the child form alone. Any solutions please?

What I have tried:

I've tried using this.Dispose() as well. That doesn't work too. If I use this.Close() there is no exception that is generated, while using this.Dispose() it throws an exception saying that : "Cannot show Disposed object".

Any ideas?

推荐答案

我认为您的应用程序正在关闭时出现以下类型的错误:



System.Windows.Forms.dll中发生'System.InvalidOperationException'类型的未处理异常



因为您正在尝试关闭正在创建的表单(应用程序创建句柄的加载事件中的子窗体。



您还应该看到这些信息:



附加信息:在执行CreateHandle()时无法调用Value Close()。



如果您没有看到此类错误消息,那么你还有别的错误。



正如Dave K.在他的解决方案中建议的那样,这里的答案永远不会执行创建子表单的代码unl ess条件满足。
I think your app is shutting down with an error of type:

"An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll"

Because you are trying to Close the Form being created (child Form) in the Load Event where the Application is creating a Handle.

You should be seeing this information also:

"Additional information: Value Close() cannot be called while doing CreateHandle()."

If you are not seeing this type of error message, then you something else is wrong.

As Dave K. suggests in his solution, the answer here is never execute the code that creates the child Form unless the condition is satisfied.


Form_Load事件不是这样做的地方。在尝试创建子表单的实例之前,您应该检查此情况。
The Form_Load event is NOT the place to do this. You should be checking for this condition before you even try to create an instance of the child form.


这篇关于关闭MDI子项将关闭父窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆