允许 Form1 关闭而不关闭 C# 中的应用程序



好的,form1是我的"介绍"。我需要表单 1 来关闭和表单 2...当它关闭时,整个应用程序将关闭。我是新手,所以我不知道如何很好地解释我的问题......如果您有任何问题..请问... :)

当我需要在显示主窗体之前有一个初始屏幕时,我使用的另一种解决方案是使用此解决方案:

在程序类的 Main 函数中,您通常有类似的东西:

    [STAThread]
    static void Main(string[] ps)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

Application.Run 将应用程序绑定到窗体,以便在窗体关闭时,应用程序退出。如果要在表单 1 之前显示表单 2,可以执行以下操作:

    [STAThread]
    static void Main(string[] ps)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Form1 splash = new Form1();
        splash.ShowDialog();
        Application.Run(new Form2());
    }
此新代码将显示 form2。

关闭 form2 后,将显示 form1。关闭 Form1 将退出应用程序。

通常,启动窗口是在另一个线程中创建的,让主线程加载所需的数据。

您可以隐藏 From1,而不是关闭。

Form1.hide = true

您应该在以下表单中重写 OnClosing 方法...

protected override void OnClosing(CancelEventArgs e)
{
     e.Cancel = true;
     this.Hide();
}

最新更新