views:

200

answers:

5

Hi

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.

A: 

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.

Diego Jancic
How about if it is not a main form? Like I if I do this -> Login -> Second Form -> Third form. Can I at least close the second form?
chobo2
The easiest way is keep one form as the main one, it can be always hidden though. You can avoid this by calling Application.Run(...); multiple times, but you are likely to loose the app context between those calls.
Diego Jancic
+1  A: 

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.

kmontgom
A: 

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()

Jacob Seleznev
+1  A: 

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.

Sorin Comanescu
A: 

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.

Quibblesome