是的,所以我想要一个密码文本框和一个确认密码文本框,但我不确定如何正确设置它们。
我有以下内容:
// 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,我无法找到答案。
答案 0 :(得分:0)
我也是初学者,但我希望我的建议能帮到你。
问题是 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.");...
或者你可以阻止输入或做类似的事情。
答案 1 :(得分:0)
使用文本框的Validating事件,而不是使用文本框的TextChanged事件来检查密码。当用户尝试将焦点更改为另一个控件时,会发生该事件。如果密码不匹配,可以通过将e.Cancel设置为true来阻止焦点更改。
答案 2 :(得分:0)
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