关闭当前表单并打开另一个表单
本文关键字:表单 另一个 | 更新日期: 2023-09-27 18:19:45
我想知道如何关闭当前C#表单并打开另一个的最佳方式
DetailForm df = new DetailForm();
df.Show();
this.Hide();
this.Dispose();
DetailForm df = new DetailForm();
df.Show();
this.Close();
但是要小心,如果关闭主窗体,应用程序将被关闭。
已编辑
如果第一个窗体是主窗体,则要运行此操作,您需要执行更多操作。试试这样的东西:
将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());
}
}
}
在主要形式中,用这个结束:
private void Foo(object sender, EventArgs e)
{
Program.OpenDetailFormOnClose = true;
this.Close();
}
如果在主窗体关闭后将OpenDetailFormOnClose
设置为true,则会调用DetailForm。