Reputation: 85845
I am wondering how do I close a form after I open up a new form.
For instance I have a login form and when they login I open a new form up but I want to close the login form down.
So how would I do this?
I am making these forms on windows mobile 6 compact edition. Not sure if it would be different then in windows forms.
Upvotes: 2
Views: 5130
Reputation: 25429
I'm crazy, but I would suggest only using one form. Make the rest of your forms UserControls and just swap them in and out of the main form. Then you don't have to worry about any of this rubbish.
My Program.cs usually looks something like:
public class Program
{
static void Main()
{
MyApp application = new MyApp();
application.Initialise();
Application.Run(application.MainForm);
}
}
This way I can keep initialisation and logic outside of forms.
Upvotes: 0
Reputation: 4867
Could do it like this:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (DialogResult.OK == new LoginForm().ShowDialog())
{
Application.Run(new MainForm());
}
}
}
Should ensure the LoginForm closes with DialogResult.OK if valid credentials and with DialogResult.Cancel otherwise:
private void ValidateLogin(...)
{
... // check credentials
if(validCredentials)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
else
{
... // up to you, maybe keep the form displayed to give user a chance to enter correct credentials
}
}
}
No need to hide anything.
Upvotes: 4
Reputation: 8151
Just call Form.Close()
.
You will need to get an object for the login form that is open, or if you are in the login form when you want to close it, try calling this.Close()
Upvotes: 0
Reputation: 1429
Most desktop apps implement login forms as modal dialogs. If you do the same thing, you would first display the main form, and then immediately display the login as a modal dialog, from the main form's form_loaded event. Once the login dialog has been closed, you can obtain login credentials from it and continue.
Upvotes: 1
Reputation: 7450
If the main form is the Login window, you will only be able to hide it, since if you close it all other child windows will be closed too. You have 2 options I guess:
1- Make your application's form, the main one. When the application starts, hide it and display the login window. When the login window is closed, show the main form.
2- Once the user enters his credentials in the login window, hide it (do not close it), and then open the other form. When the second one is closed, close both to shut the application down.
Upvotes: 1