Keydown event for whole VB.Net application

I want to make it so that no matter what control has focus, it will execute my event. So I don't need to write a keydown event for all 137 of my objects. Is it possible?

thank

+2


source to share


2 answers


You must set the KeyPreview property of your form to True.



If this property is set to true, the form will receive all KeyPress, KeyDown, and KeyUp.

+4


source


I am not an expert, but I believe it is possible. I've done this before, when I had an event handler that would handle multiple button clicks and then do something different depending on which button has focus.

The code for this is something like:

For Each ctl in Me.Controls
If ctl.Type is <whatever type of control you want to handle> And ctl.Isfocused Then
Do whatever
End If
Next

      



You don't have to worry about recursion for something like this.

It's easier if you want each control to do the same, no matter which one has focus.

Private Sub keypresshandle (System arguments ignore this) Handles key1.press, key2.press etc .... key10000.press
do some stuff here
End Sub

      

+1


source







All Articles