隐藏MDI子窗体上关闭C# [英] Hiding MDI Child Forms on Close C#

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

问题描述

我目前正在建设一个多文档界面的应用程序,但我有一个问题,当子窗体通过X按钮关闭。当窗体关闭,再次显​​示它的唯一方法是创建特定形式的新实例,这意味着所有包含在previous表单中的数据丢失。

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.

有没有解决的办法吗?

下面是我目前使用的为我的孩子形式的形式结束活动code:

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

    }

通过这个code,主窗体的X按钮必须被点击两次,一次以关闭子窗体,并一度关闭的主要形式。

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.

推荐答案

表单是的打开和关闭由用户。而且,事实上,当它们闭合时,对象实例是受被破坏,从而导致您丢失存储在与该对象实例相关联的字段或属性的所有数据。

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.

所以,你不应该使用的表单实例作为一个永久的地方来存储数据。你需要写数据到磁盘,将其保存到数据库中,或者简单地将其存储在一个类的实例跨越的所有的你的形式(即,当然也不会被破坏,直到你共享明确地这样做通过code,因为它没有用户界面和用户不能为关闭)。

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).

不过,如果你只想做这项工作,这是可以做到这一点。您需要更改code在的FormClosing 事件处理程序的只有的prevent在关闭子窗体时, e.CloseReason 属性表示,他们正在关闭作为直接用户交互的结果:

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 the e.CloseReason property indicates that they're closing as a result of direct user interaction:

private void ParameterForm_FormClosing(object sender, FormClosingEventArgs e)
{
    if (e.CloseReason == CloseReason.UserClosing)
    {
        this.Hide();
        e.Cancel = true;
    }
}

这是你的检查不工作的原因( e.CloseReason!= CloseReason.FormOwnerClosing )是因为你有一个MDI应用程序。还有就是当MDI父正在关闭使用一个特殊的原因: CloseReason.MdiFormClosing 。你可以看为的的,但它是简单的做到这一点上面显示的方式,因为你不想prevent从什么时候关闭Windows正在关闭的窗户或者,例如

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.

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

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