tags:

views:

184

answers:

4

I created a login form in c#. If a user signs in with the right password en username and clicks on 'Login' then the seconds form opens. How to close the login form after the last step?

A: 

You can type

FormName.ActiveForm.Close();

It closes the current active form.

Royson
A: 

You can call this.Close();

Stuart Dunkeld
A: 

I would do a ShowDialog() of the Login Form from the main form.

After the login form closes you are back in the main form.

    private void Form1_Load(object sender, EventArgs e)
    {

        var foo = new Form() { Text = "Login" };
        if (foo.ShowDialog() == DialogResult.OK)
        {
           ...
        }
    }
Louis Haußknecht
+1  A: 

Change your Main() method in Program.cs to display the login dialog. Don't start the message loop unless a valid login was entered. For example:

static void Main() {
  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);
  using (var login = new LoginForm()) {
    if (login.ShowDialog() != DialogResult.OK) return;
  }
  Application.Run(new Form1());
}

Your LoginForm should set its DialogResult property to OK if it detected a proper login.

Hans Passant
;-) thk u very much!!
Sjemmie