C# 关闭当前表单并打开另一个表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17296310/
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
Closing current form and open another form
提问by user2519850
I want to know how the best way to close current C# form and open another one
我想知道关闭当前 C# 表单并打开另一个表单的最佳方法
DetailForm df = new DetailForm();
df.Show();
this.Hide();
this.Dispose();
采纳答案by Jonny Piazzi
DetailForm df = new DetailForm();
df.Show();
this.Close();
But be careful, if you close the main form the application will be closed.
但要小心,如果您关闭主窗体,应用程序将被关闭。
EDITED
已编辑
To run this if the first form is the main form you need do more. Try something like this:
如果第一个表单是主表单,则要运行它,您需要做更多的事情。尝试这样的事情:
Change your Program.cs file to this:
将您的 Program.cs 文件更改为:
public static class Program
{
public static bool OpenDetailFormOnClose { get; set; }
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
OpenDetailFormOnClose = false;
Application.Run(new MainForm());
if (OpenDetailFormOnClose)
{
Application.Run(new DetailForm());
}
}
}
And in the main form, close it with this:
在主窗体中,关闭它:
private void Foo(object sender, EventArgs e)
{
Program.OpenDetailFormOnClose = true;
this.Close();
}
If you set OpenDetailFormOnClosewith true after the close of the main form, the DetailForm will be call.
如果OpenDetailFormOnClose在主窗体关闭后设置为 true,将调用 DetailForm。

