Keydown handler for many controls

Is there a better way to have a keydown handler for many controls in a busy form? (Using a form handler in some cases does not work.)

Private Sub rText_KeyDown(ByVal Sender As Object, ByVal e As KeyEventArgs) Handles rText0.KeyDown, txTaxon.KeyDown, txCommon.KeyDown, _ txConfidence.KeyDown, txDate.KeyDown, txDateAdded.KeyDown, txFileName.KeyDown, txGPS.KeyDown, txRating.KeyDown, _ txConfidence.KeyDown, txQuality.KeyDown, txRemarks.KeyDown, txKeyWords.KeyDown, txOriginalPath.KeyDown, txDateAdded.KeyDown, _ txLink.KeyDown, chkLink.KeyDown, rview.KeyDown, cmdNext.KeyDown, tvTaxon.KeyDown, Me.KeyDown, _ cmdTaxon.KeyDown Call globalkey(e) End Sub 
+4
source share
2 answers

I am a C # programmer and therefore cannot provide working solutions (as in vb.net code).

I think you can make it work by doing the following.

  • Write down the AddHandler method for your respective controls, if and only if they have a specific value set for the property (for details, see step 2 below)
  • All controls to which you want to attach a common handler, set the Tag property to, for example: "KeyDown" . I would recommend using Enum , although in this case instead of string
  • Now, in your Form constructor, after calling InitializeComponent call the method that you wrote in # 1 above. This will allow handlers to be bound to all desired controls.
  • If you have several forms that have this requirement, you can add this functionality to the main form, which acts as the parent for everyone else (inheritance)

Hope I'm clear enough and that helps.

+2
source

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.

+2
source

Source: https://habr.com/ru/post/1437113/


All Articles