Hello,

I have two forms where one is a login form and the other is the main form. The user enters their username and password and clicks the "login" button. The code in the login button validates the username and password and if correct opens the main form. The problem I'm having is that when the main form opens the login form doesn't close. I have tried using this.close() but it closes the whole application. I have tried closing the login form from the login button and in the load event of the main form but the whole application still closes.

I would appreciate any help you can offer with this problem.

Thanks in advance

SubProf

Reverse your order of opening. Open the main form (but don't show it). Then from the main form's load event open the login form. If the login is valid, show the main form and close the login form. If the login is invalid, do an Application.Exit() or close the main form.

What's happening right now is that the login form is your main form as far as .NET is concerned because it's the first form opened. When the main form closes, the application terminates.

you could hide the login form by adding the following under the instantiation of your next form(in login form class):

this.Hide();

Jx_man's solution is better than mine, as when you are debugging my solution means that the application is still running (the login form is there, just not visible). Jx_man's solution closes the login form instead of hiding it. Nice one Jx_man!

Guys,

This worked! Thanks for all your help I really appreciate it

SubProf :)

this.Dispose(false)

when I close the Second Form..! It's doesn't close the application...!!
Should we have to exit application manually using Application.Exit()??

hey thanks guys for answering this question i have been searching up since last week in google and posting it here and other websites. this is also my problem and alas! my application works already!

@best52 yes you have to manually put application.exit(); in form2 to close all the forms and terminate the application.

this.close();

My scenario is different but I also had an issue with this.Close() that was solved with the this.Dispose(false) solution. I have a main form from which a set of diagnostic forms can be launched. Let's say I launch diagnostic A which opens a form window, close the form clicking the "cancel" button, and then try to launch the same diagnostic again.

If inside the cancel button event handler I use this.Close(), the form closes but clicking on the "run diagnostic" button again

private void buttonRun_Click(object sender, EventArgs e)
{
    DiagnosticForm diagnostic = new DiagnosticForm(this._currentDiagnostic);
    diagnostic.Owner = this;
    diagnostic.Show();
}

will result in an unhandled ObjectDisposedException. If I use this.Dispose(false) like in the following event handler

private void buttonCancel_Click(object sender, EventArgs e)
{
    this.Dispose(false); 
}

I can reopen the form as man times as needed without problems.

this.Dispose(false);

MessageBox.Show("Access Granted", "Message", MessageBoxButtons.OK);
MainServerPage editor = new MainServerPage();
editor.ShowDialog();


I tried to use your method to close my login form, the login form doesnt close after I clicked OK. Instead, it proceeds to the main form with the login form still at the back.
If I change the code as follows

MessageBox.Show("Access Granted", "Message", MessageBoxButtons.OK);
this.dispose(false);
MainServerPage editor = new MainServerPage();
editor.ShowDialog();

the application exit straight away.
Can you please tell me how should I edit my code. Thanks

This question has already been answered. Start a new discussion instead.