你怎么能确定是否一个形式已被用户或您的代码关闭? [英] How can you determine whether a form has been closed by the user or by your code?
问题描述
我有一个表格(我们称之为父窗体),从另一个总在最前面,但不是模态的形式可以被加载(一个对话框喜欢,但没有一个对话框 - 用户可以将新的子窗体的地方是继续在父窗体上工作)。
I have a form (let's call it parent form), from which another "always on top but not modal" form can be loaded (Like a dialog but not a dialog - user can leave the new "child form" where it is and continue to work on the parent form).
在用户打开这个孩子形成一些数据加载和显示的第一次。他们应该关闭的形式,我居然拦截密切和隐藏简单的形式 - 这样他们下次打开它的时候,我们不必重新加载数据(它不是改变非常多,如果在所有数据)。 p>
The first time the user opens that child form some data is loaded and displayed. Should they close the form, I actually intercept the close and simply hide the form - so that next time they open it, we don't have to reload the data (it is not data that changes very much if at all).
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
this.Hide();
}
后来,当父窗体关闭,我想迫使子窗体正确关闭 - 运行一些代码在它的基本形式来存储它的下一次位置和大小
Later, when the parent form is closed, I want to force the child form to close properly - running some code in it's base form to store its location and size for next time.
目前我称之为 childForm.Dispose()从清理事情了很好,但并没有给我太多的控制父窗体。
Currently I call childForm.Dispose() from the parent form which cleans thing up nicely, but doesn't give me much control.
不过,如果我叫childForm.Close()方法,电子商务.CloseReason仍然是CloseReason.UserClosing。
However, if I call the childForm.Close() method, the e.CloseReason is still "CloseReason.UserClosing".
有没有办法关闭的形式和我的代码(在父窗体)关闭它的用户进行区分?
Is there a way to distinguish between the user closing the form and my code (in the parent form) closing it?
推荐答案
只需添加,做一个特殊的 ReallyClose()
的方法你当你真的要关闭表单清理和被调用。
Just add a special "ReallyClose()
" method that does your cleanup and is called when you really want to close the form.
这篇关于你怎么能确定是否一个形式已被用户或您的代码关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!