使用 C# 在关闭时隐藏 MDI 子窗体
我目前正在构建一个多文档界面应用程序,但是当通过 x 按钮关闭子表单时遇到问题。当表单关闭时,再次显示它的唯一方法是创建该特定表单的新实例,这意味着先前表单中包含的所有数据都会丢失。
我尝试设置表单关闭事件以简单地隐藏表单,但是当用户关闭主父表单时,应用程序不会退出。
有办法解决这个问题吗?
以下是我当前用于子表单的表单关闭事件的代码:
private void ParameterForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason != CloseReason.FormOwnerClosing)
{
this.Hide();
e.Cancel = true;
}
}
使用此代码,必须单击主表单的 x 按钮两次,一次关闭子表单,一次关闭主表单。
I'm currently building a multiple-document interface application, but I'm having a problem when the child forms are closed via the x button. When the form is closed, the only way to show it again is to create a new instance of that particular form, meaning all of the data contained within the previous form is lost.
I have attempted to set the form closing event to simply hide the form, but then when the user closes the main parent form, the application does not exit.
Is there a way around this?
Here is the code I am currently using for my child forms' form closing event:
private void ParameterForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason != CloseReason.FormOwnerClosing)
{
this.Hide();
e.Cancel = true;
}
}
With this code, the main form's x button must be clicked twice, once to close the child form and once to close the main form.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
表单旨在由用户打开和关闭。事实上,当它们关闭时,对象实例可能会被破坏,导致您丢失存储在与该对象实例关联的字段或属性中的所有数据。
因此,您不应该使用表单实例作为存储数据的永久位置。您需要将该数据写入磁盘,将其保存到数据库中,或者可能只是将其存储在所有表单之间共享的类实例中(当然,除非您这样做,否则该实例不会被销毁)通过代码显式执行此操作,因为它没有用户界面并且无法由用户“关闭”)。
但是,如果您只想完成这项工作,也可以这样做。您需要更改
FormClosing
事件处理程序中的代码,以仅阻止子表单在e.CloseReason
属性指示子表单关闭时关闭由于直接用户交互而重新关闭:您的检查不起作用的原因 (
e.CloseReason != CloseReason.FormOwnerClosing
) 是因为您有一个 MDI 应用程序。 MDI 父级关闭时会使用一个特殊原因:CloseReason.MdiFormClosing
。您也可以注意这一点,但是按照上面所示的方式执行此操作更简单,因为您不想在 Windows 关闭时阻止窗口关闭,例如。Forms are intended to be opened and closed by the user. And, indeed, when they are closed, the object instance is subject to being destroyed, causing you to lose all data that is stored in fields or properties associated with that object instance.
Therefore, you should not use form instances as a permanent place to store data. You need to write that data out to disk, save it into a database, or perhaps simply store it in a class instance shared across all of your forms (that, of course, will not be destroyed until you explicitly do so through code, as it has no user interface and can't be "closed" by the user).
However, if you just want to make this work, it's possible to do that as well. You need to change the code in your
FormClosing
event handler to only prevent the child forms from closing when thee.CloseReason
property indicates that they're closing as a result of direct user interaction:The reason that your check doesn't work (
e.CloseReason != CloseReason.FormOwnerClosing
) is because you have an MDI application. There's a special reason that is used when the MDI parent is closing:CloseReason.MdiFormClosing
. You could watch for that also, but it's simpler to do it the way shown above, because you don't want to prevent the windows from being closed when Windows is shutting down either, for example.