tags:

views:

1703

answers:

4

I am having problems closing out a C# windows form app. It will currently just give me a blank form with no title or anything. I need to find a way to close this little unknown window.

I Have 2 form pages one for a login screen and one for the actual app. All being run by a program.cs file.

program.cs

...
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new PROG1());
}

This is just the basic main created by visual studio to run my program.

Then we have the main program

PROG1.cs

...
public PROG1()
{
    Login LoginForm = new Login();
    DialogResult a = LoginForm.ShowDialog();

    if(LoginForm.ValidLogin == 1) {
        InitializeComponent();
    } else {
        Application.Exit(); //FAIL
    }
}

You can see that the first the program.cs file calls PROG1.cs which calls an instance of login.cs. If you insert a valid login the login page will close and the main PROG1 will show as it should. However if you simply click the red X to close login form it comes to Prog1 and LoginForm.ValidLogin != 1 so it does not Initialize the form and will try to close the form. This will just leave me the uninitialized form instead of closing it out. If I do a this.close() instead it will give me a runtime error.

Any Ideas?

+1  A: 

You shouldn't be doing something like this in the constructor.

Either have a new entry form that shows little or nothing and presents the login form on the Load event or from the Load event on the main form. If you show a modal dialog they cannot access your parent form and you can just close the parent if they fail to log in.

Samuel
+4  A: 

Put the InitializeComponent call back at the top where it used to be. An attempt to use uninitialized variables, including trying to tell them to close themselves, is a bad idea.

Configure the properties of your PROG1 form so the initial state will be hidden instead of shown. After the LoginForm returns, your PROG1 code can decide whether to show itself or close itself.

Windows programmer
+2  A: 

Is the reason you're not initializing the form on failure because the contents of the InitializeComponent() method take a very long time to run? It seems like you should just simplify your program to this:

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    LoginForm login = new LoginForm();

    if (login.ShowDialog() == DialogResult.OK &&
        login.ValidLogin == 1)
    Application.Run(new PROG1());
}

And remove the logic from the constructor of PROG1.

Jeremy
+1  A: 

I would recommend the best practice as Samuel suggests. But if you want something quick, try moving the login logic to the Visual Studio's generated Main:

In Program.cs

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        Form2 login = new Form2();
        if (login.ShowDialog() == DialogResult.Yes)
        {
            Application.Run(new Form1());
        }
    }

In Form2.cs

    private void button1_Click(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.No;
        this.Close();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.Yes;
        this.Close();
    }
m3rLinEz