问题
I need to set a minimum length for a password that can be used in a textbox and then set a label which will say if it meets the minimum number of characters required. I know how to set the limit of characters, what I can't do is the part where it will show in a label as soon as I leave the textbox. I was thinking I need to use an event, like maybe Leave or LostFocus, but it's not working. Please help :(
回答1:
Ok, There are plenty of ways to do what you want to achieve. I personally like to a separate subroutine; if you need to change one thing, you wont have to edit every single event that has the same code
From what I can understand, something like this should help get you on your way.
Basically, we just setup a subroutine that will check to see if textbox1.text's length is more than five and we trigger the subroutine by using events such as a button click
of if the textbox
is clicked off.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ''save button
checkPassword(TextBox1.Text)
End Sub
Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
checkPassword(TextBox1.Text)
End Sub
Private Sub checkPassword(password As String)
If Not password.Length > 5 Then
Label1.Text = "The password must be more than 5 charcharacters"
TextBox1.Clear()
Else
Label1.Text = "Password accepted"
End If
End Sub
来源:https://stackoverflow.com/questions/44497302/how-to-set-minimum-limit-of-a-textbox-vb-net