Just add the elements that will have the events treated (handled) by separating by commas:
Private Sub nct_focus(sender As Object, e As EventArgs) Handles text1.GotFocus, text2.GotFocus, text2.GotFocus.... etc....
sender.SelectAll()
End Sub
Now if you want to automatically all the Textboxes of your form have the event associated with your method, you can search all the elements within your form, in Collection Controls
, check if not of the type TextBox
and associate the event.
You can do this by loading the form, something like that:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
' Para cada controle, verificar o tipo e associar ao evento
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is TextBox Then
Dim textBox As TextBox = CType(ctrl, TextBox)
AddHandler textBox.GotFocus, AddressOf nct_focus
End If
Next
End Sub
in the first way I already knew that I could do, I wanted something close to the second form, however I had tested this way that you passed, unfortunately it did not work
– Alvaro Alves
what didn’t work? he didn’t associate the events, or associate and didn’t do what he expected?
– Ricardo Pontual
did not associate the events, I think the output will be do as in the first way
– Alvaro Alves
got.... just to confirm, if converting Sender does not work? on that line
sender.SelectAll()
Sender is an Object, no need to convert toTextBox
?– Ricardo Pontual
I managed to make it work, it was an error of my analysis, there was a tabcontrol inside my form, which made the event was not added, because there was no textbox directly in the form, but in the tabcontrol.
– Alvaro Alves
Cool, this reminds me of a lot of things, it’s been a while since I’ve worked with Forms.. Good luck :)
– Ricardo Pontual