3

I have a simple program that runs, but before that, I pop out a login form that validates if the information is correct. If it is, the login form closes and the main form kicks in. However, when I close the login form after opening the main form, it shuts down the whole application.

My program.cs just runs a new frmLogin. This is what happens when I login.

frmMain frm = new frmMain(UsernameTextBox.Text, PasswordTextBox.Text);
frm.Show();
this.Close();

How can I suppress that? Thanks

Bridge
  • 29,818
  • 9
  • 60
  • 82
  • 1
    you need to add a separate line to open it in your program.cs, when you close a dialog's parent, you close the dialog with it, or do it the other way, have the login form load when you open your other form, if invalid login, close both forms – Sayse Aug 12 '13 at 14:20

4 Answers4

1

You shouldn't have your login form set as your main form, since it won't be active for the majority of the time your application is running, and you have another form that is logically the main form of your application.

Go to the program.cs file and set your main form to be the form in Application.Run, so that it really is the application's main form. Within your main form's Load event Hide it and use ShowDialog to show your login form. When your login form is closed, you can then continue loading your main form.

Servy
  • 202,030
  • 26
  • 332
  • 449
0

I will suggest you to use a Panel instead of a new form. Place your login controls on the Panel and show it first. If user login is successful, then just make the panel invisible or remove it from the Form. Then make the panel with main form controls visible.

if(IsLoginSuccess)
{
    panel.Visible = false;
    mainFormControlsPanel.Visible = true;
}

Moreover it it should be faster than your approach I believe.

Hope it helps.

Sandy
  • 11,332
  • 27
  • 76
  • 122
  • 1
    It is highly confusing to have a single form that performs several entirely different functions through the swapping out of all of its content. Since he logically has two different forms, having two real forms is desirable. – Servy Aug 12 '13 at 14:24
0

I would suggest something like:

static void Main(string[] args) {
        var formLogin = new FormLogin();
        formLogin.ShowDialog();
        Application.Run(new FormMain(formLogin.UserName, formLogin.Password));
}
Ondrej Svejdar
  • 21,349
  • 5
  • 54
  • 89
0

You need to set up your program.cs to do it more like this:

DialogResult result = DialogResult.Cancel;

while (result != DialogResult.OK)
{
    using (LoginForm loginForm = new LoginForm())
    {
        result = loginForm.ShowDialog();

        if (result != DialogResult.OK) continue;

        if (!AuthenticateCredentialsSomehow.Authenticate(loginForm.Username, loginForm.Password))
        {
            MessageBox.Show("Login Failed!");
            result = DialogResult.Cancel;
            continue;
        }

        MainForm mainForm = new MainForm(loginForm.Username, loginForm.Password);
        Application.Run(mainForm);
    }
}

This way your login form will go away when you are done with it and the application will run on your main form once login has succeeded.

Ashigore
  • 4,618
  • 1
  • 19
  • 39