I like AYK's answer. You might use a function like this :
Public Shared Function GetAllControlsRecurs(ByVal list As List(Of Control), _
ByVal parent As Control, ByVal ctrlType As System.Type) As List(Of Control)
If Parent Is Nothing Then Return list
If Parent.GetType Is ctrlType Then
list.Add(Parent)
End If
For Each child As Control In Parent.Controls
GetAllControlsRecurs(list, child, ctrlType)
Next
Return list
End Function
I find this is a handy function to get all controls (including control within controls) of a given type in some parent control. By tagging your controls as AYK has suggested (ie: set Tag
property in designer) you can do a run through all the controls above and programmatically add handlers (probably in the constructor).
Dim textboxList As New List(Of Control)
For Each ctl As TextBox In GetAllControlsRecurs(textboxList, Me, GetType(TextBox))
If ctl.Tag = MyTags.rTextKD then
AddHandler ctl.KeyDown, AddressOf rText_KeyDown
End If
Next
Where you might define MyTags
as an enum with a list of common handlers you want to implement. Here rTextKD
would be a member of the enum (i've not defined here in the answer). The nice thing about this approach is that it is extensible - if you add a new control and tag it then this code will pick it up and hook up the handler without needing to be changed.
While the above is an answer to your direct question, if you are trying to make a global hotkey, however, this is not the way to do it. The link Hans provided in comment is probably where you want to go.