C# Winform Forms 关闭和打开一个新表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1677528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Winform Forms Closing and opening a new form
提问by Karthick
1. frmHome frm = new frmHome();
frm.Show();
this.Close();
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.
我HomeForm
从LoginForm
. 在LoginForm
的form_closed
情况下我的呼叫Application.Exit()
。这允许您LoginForm
通过单击X
按钮打开和退出应用程序。
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.
问题是当我从LoginForm
to移动到HomeForm
并调用时this.Close()
,触发器form_closed
事件LoginForm
和应用程序被关闭。
I am allowed to show only one form at a time.
我一次只能展示一种表格。
回答by Aaron Daniels
One way to accomplish this:
实现此目的的一种方法:
- Open the main form on startup.
- Hide it. (optional really, but not for you if you really can't show more than one form.)
- Open your login form with ShowDialog();
- If login is successful, then show your main form. If not, then close your main form / the application.
- 在启动时打开主窗体。
- 把它藏起来。(确实是可选的,但如果您真的不能显示多个表格,则不适合您。)
- 使用 ShowDialog() 打开您的登录表单;
- 如果登录成功,则显示您的主表单。如果没有,请关闭您的主表单/应用程序。
回答by manji
you can use a boolean (global variable) as exit flag in LoginForm
您可以使用布尔值(全局变量)作为退出标志 LoginForm
initialize it to :
将其初始化为:
exit = true;//in constructor
set it to false before closing:
在关闭之前将其设置为 false:
frmHome frm = new frmHome();
frm.Show();
exit = false;
this.Close();
and in form_closed
:
并在form_closed
:
if(exit) Application.Exit();
if a user closes the form with the 'X'
button, exit
will have the value true
, and Application.Exit()
will be called.
如果用户使用'X'
按钮关闭表单,exit
将具有值true
,并Application.Exit()
会被调用。
EDIT:
编辑:
the above is not working because LoginForm
is your main form used by Application.Run(loginForm)
.
以上不起作用,因为LoginForm
您的主要形式是Application.Run(loginForm)
.
2 suggestions:
2 建议:
With exit
flag:
带exit
标志:
replace
代替
Application.Run(new LoginForm())
by
经过
LoginForm loginFrm = new LoginForm();
loginFrm.Show();
Application.Run();
Without exit
flag:
无exit
标志:
replace in your current code:
替换您当前的代码:
frmHome frm = new frmHome();
frm.Show();
this.Close();
by
经过
frmHome frm = new frmHome();
this.Visible = false;
frm.ShowDialog();
this.Close();
回答by Glenn
Why don't you just stop calling application.exit in the form_closed event?
为什么不停止在 form_closed 事件中调用 application.exit 呢?
I am not sure that you really need it anyway, and if you do then you can remove the x icon from the screen and give them a close button.
我不确定你是否真的需要它,如果你这样做了,那么你可以从屏幕上删除 x 图标并给他们一个关闭按钮。
Alternatively there is a CloseReason in the event args that will tell you if it is a user closing the form or code or something else.
或者,事件 args 中有一个 CloseReason,它会告诉您是否是用户关闭表单或代码或其他内容。
回答by AngryHacker
In program.cs:
在 program.cs 中:
void Main() {
frmLogin fl = new frmLogin();
if (fl.ShowModal() == DialogResult.Ok) {
frmHome fh = new frmHome();
fh.Show();
}
}
回答by davidsleeps
I'm not quite sure I'm understanding this, perhaps you could do something like this if you want to loop through a specific order of forms: (pseudo code)
我不太确定我是否理解这一点,如果您想遍历特定的表单顺序,也许您可以这样做:(伪代码)
Dim formsList as New List(Of form)
formsList.add(Form1)
formsList.add(Form2)
formsList.add(Form3)
' etc etc etc '
For Each f as Form in formsList
f.ShowDialog()
' you could have a condition here which breaks the for loop perhaps '
Next
Me.close ' close the app '
You could add a condition into the For loop which breaks the for loop to end early...
您可以在 For 循环中添加一个条件,该条件会中断 for 循环以提前结束...
note: sorry for the vb.net code...but it should be easy to understand though
注意:对不起 vb.net 代码......但它应该很容易理解
回答by Skintkingle
you can force the form to hide, instead of close. instead of catching the form_closed event, catch the form_closing event. it would look something like this.
您可以强制表单隐藏,而不是关闭。与其捕获 form_closed 事件,不如捕获 form_closed 事件。它看起来像这样。
private void LoginFrm_Closing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
this.Hide();
frmHome frm = new frmHome();
frm.Show();
}
this will keep both open, but only one visible. somewhere in frmHome, possibly a public variable to hold the LoginFrm, so you can toggle between the two with Hide(); and Show(); (and any other forms you may wish to add)
这将使两者保持打开状态,但只有一个可见。frmHome 中的某个地方,可能是一个公共变量来保存 LoginFrm,因此您可以使用 Hide() 在两者之间切换;和显示();(以及您可能希望添加的任何其他表格)
Edit: Grammar.
编辑:语法。
回答by Rohan Loomis
All you need to do is this it closes the 2 forms without problem. Form1 fin = new Form1(); fin.Close(); this.Visible = false; Form2 win = new Form2(); win.Visible = true;
您需要做的就是它可以毫无问题地关闭 2 个表单。Form1 鳍 = 新 Form1(); fin.Close(); this.Visible = false; Form2 win = new Form2(); win.Visible = true;
回答by Ahmed Tarek
try that it might help you
尝试它可能会帮助你
frmHome frm = new frmHome();
this.hide()
frm.ShowDialog();
this.Close();