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 :(
- 1Perhaps provide some code to accompany your question?IronAces– IronAces2017-06-12 10:38:31 +00:00Commented Jun 12, 2017 at 10:38
- Hi, @DanielShillcock, I have already deleted my code since it wasn't working, and I knew it's wrong. I recently made a code for a textbox to only accept numbers, I tried to do the same for this but I was thinking that the Keypress event is incorrectnothere– nothere2017-06-12 10:40:41 +00:00Commented Jun 12, 2017 at 10:40
Add a comment |
1 Answer
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