开发者

Form closing immediately after show in C#

开发者 https://www.devze.com 2023-02-17 12:57 出处:网络
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:

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开发者_Python百科 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.


Solved this problem using form.ShowDialog() instead of form.Show()

0

精彩评论

暂无评论...
验证码 换一张
取 消