开发者

Creation of multiple forms using windows forms in C#?

开发者 https://www.devze.com 2023-03-03 06:09 出处:网络
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

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.SetCompatibleTextRenderi开发者_开发知识库ngDefault(false);
 LoginForm form=new LoginForm(); 
if(form.showDialog()==DialogResult.OK) 
{ 
form.close();
Application.Run(new BaseForm()); 
}
else
form.close();
}


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


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消