【问题标题】:Microsoft Visual Studio 2013 Confirm Password TextBoxMicrosoft Visual Studio 2013 确认密码文本框
【发布时间】:2015-11-19 20:39:04
【问题描述】:

好吧,我想要一个密码文本框和一个确认密码文本框,但我不确定如何正确设置它们。

我有以下几点:

    // This is where the user will enter the password for the product owner they wish to add.
    private void textBoxPassword_TextChanged(object sender, EventArgs e)
    {
        // Each character of the password will display as an asterisk.
        textBoxPassword.PasswordChar = '*';
        // Control to allow a max password length of 15 charachers.
        textBoxPassword.MaxLength = 15;
    }

    // This is where the user will re-enter the password for the product owner they wish to add.
    private void textBoxConfirmPassword_TextChanged(object sender, EventArgs e)
    {
        // Each character of the password will display as an asterisk.
        textBoxConfirmPassword.PasswordChar = '*';
        // Control to allow a max password length of 15 charachers.
        textBoxConfirmPassword.MaxLength = 15;
        // If statements to ensure that the passwords are the same.
        if (textBoxPassword.Text != textBoxConfirmPassword.Text)
        {
            MessageBox.Show("Passwords do not match.");
            textBoxConfirmPassword.Focus();
            return;
        }
    }

在某种程度上它可以正常工作。唯一的问题是,当我开始在确认密码字段中输入时,输入每个字符后都会显示“密码不匹配”消息。我只希望它显示在字符串的末尾。

我确信解决方案很简单,但我正在自学如何使用 Visual,但我找不到答案。

【问题讨论】:

    标签: visual-studio visual-studio-2013 passwords


    【解决方案1】:

    我也是初学者,但希望我的建议对你有所帮助。

    问题是 textBoxConfirmPassword_TextChanged 会在您每次输入符号时检查密码的匹配。要修复它,我可以提供添加一些代码: 而不是

    if (textBoxPassword.Text != textBoxConfirmPassword.Text)
    

    试试

    if (textBoxPassword.TextLenght==textBoxConfirmPassword.Textlength && textBoxPassword.Text != textBoxConfirmPassword.Text)
    {   
    // does not match    
    }
    

    因此,当您在 textBoxConfirmPassword 中达到与 textBoxPassword 中相同数量的符号时,它会显示是否匹配。但是你可以在消息错误之后添加符号,所以我建议在显示错误消息之前清除 textBoxConfirmPassword.Text,像这样

    textBoxConfirmPassword.Text="";
    MessageBox.Show("Passwords do not match.");...
    

    或者你可以阻止输入或做类似的事情。

    【讨论】:

      【解决方案2】:

      不要使用文本框的 TextChanged 事件来检查密码,而是使用文本框的 Validating 事件。该事件在用户尝试将焦点更改到另一个控件时发生。如果密码不匹配,您可以通过将 e.Cancel 设置为 true 来阻止焦点更改。

      【讨论】:

      • 我将如何进行编码?
      【解决方案3】:
      Private Sub txtpassword_TextChanged(sender As Object, e As EventArgs) Handles txtpassword.TextChanged
          txtpassword.PasswordChar = "*"
          txtpassword.MaxLength = 16
      End Sub
      
      Private Sub txtconfirmpassword_TextChanged(sender As Object, e As EventArgs) Handles txtconfirmpassword.TextChanged
          txtconfirmpassword.PasswordChar = "*"
          txtconfirmpassword.MaxLength = 16
      
          If txtconfirmpassword.Text.Length = txtpassword.Text.Length Then
              If txtconfirmpassword.Text <> txtpassword.Text Then
                  MsgBox("Password do not match")
              End If
          End If
      End Sub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多