I like the answer of AYK. You can use this function:
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 believe that this is a convenient function for getting all controls (including controls inside controls) of a certain type in any parent control. By marking your controls as suggested by AYK (i.e., setting the Tag property in the constructor), you can execute all the controls described above and programmatically add handlers (possibly to 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 can define MyTags as an enumeration with a list of regular handlers that you want to implement. Here rTextKD will be a member of the enumeration (I have not defined here in the answer). The best part about this approach is the extension - if you add a new control and mark it, this code will pick it up and attach the handler without requiring a change.
Although the above is the answer to your direct question, however, if you are trying to create a global hotkey, this is not the way to do it. The link provided in the comment is probably where you want to go.
J ... source share