Winform的窗体关闭和开启的新形式 [英] Winform Forms Closing and opening a new form

查看:120
本文介绍了Winform的窗体关闭和开启的新形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1. frmHome frm = new frmHome();
   frm.Show();
   this.Close();



我打开 HomeForm LoginForm的。在 LoginForm的 form_closed 事件我称之为 Application.Exit()。这可以让你通过点击 X 键打开 LoginForm的和退出应用程序。

I'm opening HomeForm from LoginForm. In LoginForm's form_closed event I call Application.Exit(). This allows you to open LoginForm and exit the application by clicking the X button.

现在的问题是,当我从 LoginForm的移至 HomeForm 并调用 this.Close() LoginForm的触发器的 form_closed 事件和应用程序获取关闭。

The problem is when I move from LoginForm to HomeForm and call this.Close(), the form_closed event of LoginForm triggers and the application gets closed.

我允许一次显示只有一种形式。

I am allowed to show only one form at a time.

推荐答案

您可以使用一个布尔(全局变量)的出口标志在 LoginForm的

you can use a boolean (global variable) as exit flag in LoginForm

其初始化为:

exit = true;//in constructor

设置为false闭幕前:

set it to false before closing:

frmHome frm = new frmHome();
frm.Show();
exit = false;
this.Close();



form_closed

if(exit) Application.Exit();

如果用户关闭与'X'的形式按钮,退出将具有值真正 Application.Exit() 将被称为

if a user closes the form with the 'X' button, exit will have the value true, and Application.Exit() will be called.

以上是行不通的,因为 LoginForm的 Application.Run(登录表单)使用您的主要形式

the above is not working because LoginForm is your main form used by Application.Run(loginForm).

2的建议:

使用退出标记:

替换

Application.Run(new LoginForm())

LoginForm loginFrm = new LoginForm();
loginFrm.Show();
Application.Run();



没有退出标志:

在你当前的代码替换:

frmHome frm = new frmHome();
frm.Show();
this.Close();



by

frmHome frm = new frmHome();
this.Visible = false;
frm.ShowDialog();
this.Close();

这篇关于Winform的窗体关闭和开启的新形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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