How to create winform login dialog and keep looping

后端 未结 4 1080
庸人自扰
庸人自扰 2021-01-20 14:21

Creating a login form that will then proceed to the main form if the credentials are correct. Here is the basic pseudocode:

ShowLoginForm()

if (DialogResul         


        
相关标签:
4条回答
  • 2021-01-20 14:53

    why not create main form and then show login modal dialog. Then you could check what to do.

    0 讨论(0)
  • 2021-01-20 14:53

    How about setting the DialogResult to None in the SubmitButton event handler? Something like:

    private void loginButton_Click(object sender, EventArgs e)
    {
        if (isValidCredentials())
          DialogResult = DialogResult.OK;
        else
        {
          MessageBox.Show("Failed to login or some other error");
          DialogResult = DialogResult.None;
        }
    }
    
    0 讨论(0)
  • 2021-01-20 15:08

    Try this

    LoginForm loginForm = new LoginForm();     
    do {
       if (loginForm.ShowDialog() == DialogResult.Cancel) {
           return; // Ends application
       }
    } while (CheckCredentials() != VALID)
    Application.Run(new AutoSignerForm());
    
    0 讨论(0)
  • 2021-01-20 15:15

    I would do the following:

    • in Program.cs show the loginForm as a dialog

      LoginForm login_form = new LoginForm();
      if(login_form.ShowDialog() == DialogResult.OK) {
          Application.Run(new MainForm());
      }
      
    • in LoginDialog, handle the click event on "Login" button (or whatever is named)

      // replace with the actual login
      if(textBoxUsername.Text == "my user" && textBoxPassword.Text == "my pass") {
         // save the user has logged in somewhere
         // set the dialog result to ok
         this.DialogResult = DialogResult.OK;
         // close the dialog
         this.Close();
      } else {
         // login failed
         MessageBox.show("Login failed");
         // do not close the window
      }
      

    Now, you will keep the LoginDialog shown until the user enters valid credentials or he gives up trying and the application closes. This way you will have only one instance of the LoginForm and it will be a nice user experience. Also, you can be sure that the MainForm is not initialized and shown without the user logging in successfully.

    0 讨论(0)
提交回复
热议问题