user186246
user186246

Reputation: 1877

Creation of multiple forms using windows forms in C#?

I want to create two forms: LoginForm and BaseForm.

LoginForm is created first and it contains 'Login' and 'cancel' button . When user clicks 'Login' button then LoginForm should be closed and BaseForm should be created and opened.

When user clicks 'cancel' button of LoginForm then it should get closed.

How can I do it? Can anyone please give me good solution for this?

Solution:


In the main():

Application.EnableVisualStyles();
 Application.SetCompatibleTextRenderingDefault(false);
 LoginForm form=new LoginForm(); 
if(form.showDialog()==DialogResult.OK) 
{ 
form.close();
Application.Run(new BaseForm()); 
}
else
form.close();
}

Upvotes: 1

Views: 2347

Answers (4)

tanasi
tanasi

Reputation: 1804

private void buttonLogIn_Click(object sender, EventArgs e)
    {
        LoginForm loginForm = new LoginForm ();
        loginForm .Show();
        this.Hide();
    }



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

Upvotes: 1

user1921
user1921

Reputation:

I have an app that requires login and what I do in the initialization method is check to see if the user is logged in. If they are not I open a Modal with the login prompt. If they cancel that I close the app.

The code to show the modal:

 var login = new Login();
 login.ShowDialog();

The cancel button on login would call something like:

 Application.Exit();

Upvotes: 1

David Heffernan
David Heffernan

Reputation: 613612

In your application's main function:

  1. Create the login form and use ShowDialog() to show it modally.
  2. Get the requested action from the value returned by ShowDialog().
  3. If the user pressed cancel, then return from the main form and thus close the application.
  4. Otherwise create the main form and pass it to Application.Run().

If you do it in this order then the main form won't be shown until your user has finished with the login form.

Upvotes: 0

Johann Blais
Johann Blais

Reputation: 9469

You could simply open the main form, check if the user is authenticated. Then show the login form and if the user cancels the login form, just close the application.

Upvotes: 0

Related Questions