C# Closing a form during a constructor

Posted by pm_2 on Stack Overflow See other posts from Stack Overflow or by pm_2
Published on 2010-06-18T07:39:34Z Indexed on 2010/06/18 7:43 UTC
Read the original article Hit count: 201

Filed under:
|
|

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 partial class MyForm : Form
{        
    public MyForm()
    {
         if (MyFunc())
            {
                this.Close();
            }
       }
}

Which errors in Main(), here:

    static void Main()
    {            
        ...

    // Following line errors
        Application.Run(new MyForm());
    }

I’ve tried checking the result of MyForm like this:

    static void Main()
    {            
            ...

            MyForm frm = new MyForm();
            if (frm != null)
            {
          // Following line errors
                  Application.Run(frm);
            }
    }

But that doesn’t seem to help. Can anyone tell me a way around this, please? Maybe a way to check the form to see if it still exists?

© Stack Overflow or respective owner

Related posts about c#

Related posts about winforms