Application.Run()没有参数

时间:2013-02-16 19:24:00

标签: c# winforms

有人在这里提出了类似的问题C# Application.Run without Form

但是经过检查的答案不适合我。

我想做的是:

static void Main()
{
     Application.EnableVisualStyles();
     Application.SetCompatibleTextRenderingDefault(false);
     ObjController objController = new ObjController();
     Application.Run();
     Application.Exit();
}

class ObjController
{

    Form1 form1 = new Form1();       

    public Form1 showObj()
    {
        form1.Show();
        return new Form1();        
    }
}

但是,当Application.Exit()中的表单main()关闭时,似乎无法执行。为什么?我该怎么做?

1 个答案:

答案 0 :(得分:1)

您需要“监听”FormClosed事件并在那里调用Application.Exit():

class ObjController
    {

        Form1 form1 = new Form1();


        public Form1 showObj()
        {
            form1.FormClosed += form1_FormClosed;

            form1.Show();
            return new Form1();

        }

        void form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }
    }