How to get currently selected control?

  • Thread starter Thread starter mdb
  • Start date Start date
M

mdb

I have a fairly complicated windows forms with user controls and panels and
everything. I'm using KeyPreview in my main form to watch keyboard events
because I need to have some keys that work 'globally'. For example, I need
to catch the '1', '2', '3', and Backspace keys. When these keys are
pressed, my program takes some action.

However, I don't want the program to take action when the user is actually
typing in a textbox. Thus, I need some way to query what the active
control is - at the lowest level. That is, if I have a text box in a user
control inside another user control, I need a function that will return an
instance of 'Control' that indicates what control has focus, no matter how
deep the control is...

-mdb
 
Did you try using the ActiveControl property? This will return the currently
active control on the container control. You could use it directly off the
form (Me.ActiveControl) and it will return the control that is active (If a
control has focus, it is indeed active).

hope that helps..
Imran.
 
Back
Top