C#GUI多形式子父母 [英] c# gui multi forms child parent
本文介绍了C#GUI多形式子父母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,我是C#的初学者,我有一个具有2种形式的GUI应用程序,当我关闭孩子时,我想能够
父表单也会自动关闭吗?
谢谢.
Hello i am a beginner at c#, I have a GUI application with 2 forms and i wanna be able when i close the child that the
parent form will automatically close also?
Thanks.
推荐答案
看看使用Eventhandler处理关闭事件(如^ ]).
Have a look at using an Eventhandler for the close event (as described here[^]).
句柄 ^ ]事件.
在您的代码中,您可以执行以下操作:
Handle FormClosed[^] event.
In your code, you can do something like this:
LoginForm loginForm = new LoginForm();
loginForm.FormClosed += new FormClosedEventHandler(this.LoginForm_FormClosed);
...
void LoginForm_FormClosed(object sender, FormClosedEventArgs e)
{
MessageBox.Show("LoginForm has closed, with reason: " + e.CloseReason);
}
让我们的方法符合逻辑.如果只有两种形式,并且您想同时关闭这两种形式,则意味着您要退出该应用程序.现在,其中一种形式是主要形式.它在调用Application.Run
时定义.用作参数的形式成为主要形式.如果您关闭主窗体应用程序,则仍然存在.您只需要注意第二种形式.
覆盖其虚拟OnFormClosed
方法:
Let''s approach logically. If there are only two forms, and you want to close both, it means you want to exit the application. Now, one of the forms is a main one; it it defined whenApplication.Run
is called. The form used as an argument becomes main. If you close a main form application exist anyway. You only need to take care about your second form.
Overrides its virtualOnFormClosed
method:
//only for non-main form:
protected override void OnFormClosed(FormClosedEventArgs e) {
Application.Exit();
}
这篇关于C#GUI多形式子父母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文