If your login form is the startup form from your project, then when it closes, your application does as well - so any other forms it opens will also be closed.
Probably the best way to handle this is pretty simple - when your user correctly enters the login details:
MainForm mf = new MainForm();
Hide();
mf.ShowDialog();
Close();
This way, the code will stop until the mail form is closed, but the login form will no longer be visible. The login form (and your app) will close when your main form does.
"okay uhm il try that code now, see cos in the 2nd form ill be having a switch user button which re opens the login form"
That's also easy to handle:
You can set a return value from the main form and inspect that in the login form, either via the DialogResult or via a "NewUser" bool in your main form.
For example:
MainForm mf = new MainForm();
Hide();
mf.ShowDialog();
if (mf.NewUser)
{
Show();
}
else
{
Close();
}
Using the DialogResult is the same:
MainForm mf = new MainForm();
Hide();
if (mf.ShowDialog() == DialogResult.OK)
{
Show();
}
else
{
Close();
}
And you set the "Switch User" button to have the DialogResult of OK and it'll close the form and return OK to your login form.
"now it doesnt re open the login form -_- ugh ima noob"
OK - go back a step.
This is the code in your "SwitchUser" button handler, yes?
The simplest way to do this is to do it manually to start with.
If you want to show the login form again, then set the main form DialogResult:
private void SwitchUserButton_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Are You Sure To Exit ?", "Exit", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
DialogResult = DialogResult.OK;
}
}
The main form will close if you press "yes" and it will return OK.
If you press no, it won't close or return anything.
If you close the main form in any other way, it will return "Cancel"
(You can make the button do it automatically by setting the properties, but then you have to "cancel" it if the user says "No")