Hi!
I'm trying to find out how to automaticly click button1 when textbox1 has 10 characters? Is this even possible?
Thank you!
Printable View
Hi!
I'm trying to find out how to automaticly click button1 when textbox1 has 10 characters? Is this even possible?
Thank you!
I believe you could use the TextChanged event of your text box. Each time the content of your text box is changed the event is called. Then you check if your textbox length is 10 and call the method you want if it is. I'll run a quick test and let you know if it works.
Thanks for your time!
I tried this:
But it didn't work..Code:Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.SelectionLength = "10" Then
Button1.Click()
End If
End Sub
Yeah it does work but you need to call Button1_Click instead of Button1.Click()
vb.net Code:
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click MessageBox.Show("Button1 has been clicked!") End Sub Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged If TextBox1.Text.Length >= 10 Then Button1_Click(Button1, Nothing) End If End Sub End Class
Edit : Note that I used ">=" in case you use copy/paste to paste text in your textbox. Pasting only calls TextChanged once, so if you paste more than 10 character using "=10" will return false.
Syntax error...
Quote:
If TextBox1.SelectionLength = "10"
The rest is on stlaural code in the previous post :)Code:If TextBox1.SelectionLength = 10
stlaural, you could just do Button1.PerformClick() or if you prefer calling the method directly pass it an EventArgs.Empty instead of Nothing.
Code:1.
Public Class Form1
2.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
3.
MessageBox.Show("Button1 has been clicked!")
4.
End Sub
5.
6.
Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
7.
If TextBox1.Text.Length >= 10 Then
Button1.PerformClick()
'Or:
Button1_Click(Button1, EventArgs.Empty)
9.
End If
10.
End Sub
11.
End Class
Thanks for the help guys! I got it now. ;)