关闭主窗体 [英] Close Main Form
问题描述
我使用C#Windows窗体开发一个简单的应用程序。主窗体打开另一种形式,但我不希望这两种形式。我想这是第二种形式打开时,第一个窗体关闭。由于第一种形式是主用
I am developing a simple app using c# Windows forms. The main form open another form, but i dont want the both forms. I want it that when the second form opens the first form closes. Since the first form is the main using
this.Close();
显示第二种形式后都将关闭。所以我用这个代替
after showing the second form will close the both. So i used this instead
private void btnSubmit_Click(object sender, EventArgs e)
{
frmData QS = new frmData();
QS.Show();
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
}
private void frmData_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
我想知道是否有任何其他方式做到这一点。
I want to know if there is any other way to do this.
任何帮助将不胜感激。
推荐答案
千万不要错过你的主要形式作为参数传递给 Application.Run
:
Do not pass your main form as argument to Application.Run
:
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
MainForm frmMain = new MainForm();
frmMain.Show();
Application.Run();
这样,你就可以显示出另一种形式的时候将其关闭:
Thus you will be able to close it when showing another form:
private void btnSubmit_Click(object sender, EventArgs e)
{
frmData QS = new frmData();
QS.Show();
this.Close();
}
要关闭应用程序,你应该使用
To close application you should use
Application.Exit();
这篇关于关闭主窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!