简体   繁体   中英

Form closing immediately after show in C#

I'm having a bit of trouble with a form it's a form designed with the form designer and my project, it closes immediately upon showing. Here's the relevant code:

namespace Grapher
{
    class Program
    {
        static void Main(string[] args)
        {
            InputForm mainForm = new InputForm();
            mainForm.Show();
        }
    }
}

I've tried to put in a for(;;) but that just makes the for hang, I'm probably doing something silly, very new to C#.

Thanks in advance.

Use Application.Run() :

namespace Grapher
{
    class Program
    {
        static void Main(string[] args)
        {
            Application.Run(new InputForm());
        }
    }
}

Do:

Application.Run(mainForm);

This will start the UI properly.

You need to call Application.Run(new InputForm()) .

Your code simply shows the form, then the program reaches its end (the end of the Main function) and terminates.

使用form.ShowDialog()而不是form.Show()解决了这个问题

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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