关于在winforms中关闭表单构造函数中的表单的问题

时间:2015-11-25 06:28:17

标签: c# winforms

我的要求是我需要在表单显示之前关闭表单,如果它不满足某些条件。为此,我编写了显示的事件,但它正在代码中显示的一个地方工作

我写的代码是

public form1()
{
   Initialize component();
   if(some condition)
   {
      if(some condition)
      {
             if(some condition)
             {
             }
             else
             {
              this.Shown += new EventHandler(MyForm_CloseOnStart);//here form is displaying event is not working
             }
      }
      else
      {
         this.Shown += new EventHandler(MyForm_CloseOnStart);//here only form is closing before displaying the form
      }
   }
   else
   {
      if(some condition)
      {}
      else
      {this.Shown += new EventHandler(MyForm_CloseOnStart);}//here form is displaying shown event is not working

   }

}
    public void MyForm_CloseOnStart(object sender, EventArgs e)
    {
        this.Close();
    }

1 个答案:

答案 0 :(得分:0)

假设我想根据flag变量的值关闭我的表单。然后:

    public Form1(Boolean flag)
    {
        try
        {
            if (flag)
                InitializeComponent();
            else
                Environment.Exit(0);
        }
        catch (Exception)
        {  
        }

    }