C#GUI多形式子父母

编程入门 行业动态 更新时间:2024-10-12 20:22:28
本文介绍了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[^]).

句柄 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(); }

—SA

—SA

更多推荐

C#GUI多形式子父母

本文发布于:2023-11-12 11:04:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581312.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:形式   父母   GUI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!