Closing a form during a constructor

前端 未结 9 2044
面向向阳花
面向向阳花 2020-12-01 11:59

Is it possible to close a form while the constructor is executing (or simply to stop it showing at this stage)?

I have the following code:

public par         


        
9条回答
  •  一整个雨季
    2020-12-01 12:28

    I found adding a handler to the 'Load' event is better as this way the dialog is never displayed at all. With the 'Shown' event you might briefly see the dialog open and then close which may be confusing:

    public partial class MyForm : Form
    {        
        public MyForm()
        {
            if (MyFunc())
            {
                this.Load += MyForm_CloseOnStart;
            }
        }
    
        private void MyForm_CloseOnStart(object sender, EventArgs e)
        {
            this.Close();
        }
    }
    

提交回复
热议问题