简体   繁体   English

是否允许在不关闭C#中的应用程序的情况下关闭Form1?

[英]Allow Form1 to close without closing Application in C#?

Ok, form1 is my "intro". 好的,form1是我的“简介”。 I need form1 to close and form2...When it closes, the Whole application closes. 我需要form1关闭和form2 ...关闭时,整个应用程序关闭。 I am new at this, so i do not know how to explain my problems well...If you have any question..please ask... :) 我是新来的,所以我不知道如何很好地解释我的问题...如果您有任何问题..请询问... :)

Another solution which I use when I need to have a splash screen before showing the main form is to use this solution: 在显示主要表单之前需要启动屏幕时,我使用的另一个解决方案是使用以下解决方案:

In the Main function of your Program class you usually have something like that: 在Program类的Main函数中,通常具有以下内容:

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

    }

The Application.Run bind the application to a form so that when the form closes, the application exit. Application.Run将应用程序绑定到窗体,以便在窗体关闭时,应用程序退出。 If you want to show form 2 before form 1, you can do this: 如果要在表格1之前显示表格2,可以执行以下操作:

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

This new code will show form2. 此新代码将显示为form2。 and after you close form2, it will show form1. 在关闭form2之后,它将显示form1。 Closing Form1 will exit the application. 关闭Form1将退出应用程序。

Usually the Splash window is created in another thread, leaving the main thread loading the data it needs. 通常,“启动”窗口是在另一个线程中创建的,而主线程则留有需要的数据。

除了关闭,您还可以隐藏From1。

Form1.hide = true

You should override the OnClosing method in the form... 您应该以以下形式覆盖OnClosing方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM