本文介绍了C#GUI多形式子父母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
—SA
—SA
你好,我是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[^]).
句柄 FormClosed [ ^ ]事件. 在您的代码中,您可以执行以下操作: 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 when Application.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 virtual OnFormClosed method: //only for non-main form: protected override void OnFormClosed(FormClosedEventArgs e) { Application.Exit(); }
更多推荐
C#GUI多形式子父母
发布评论