How Determine Which Control has Focus? #2

  • Thread starter Thread starter Liam McNamara
  • Start date Start date
L

Liam McNamara

I am having the same problem as the one I found in google below:

http://groups.google.com/groups?hl=...&selm=%23LO32NWECHA.1968%40tkmsftngp02&rnum=3

I need to determine which component has focus. When I select from the
main menu File->Save my validation is not called (the menu doesn't cause
the component to lose focus).

Unlike the above program, mine has over 40 controls in a single tab,
never mind the entire application. Is there an easier way than the
nightmare of each control setting an application-wide variable to say it
has focus?

--Liam.
 
Liam,

You can import the GetFocus API method like this:

[DllImport("user32.dll")]
public static extern IntPtr GetFocus();

Once you have that, you can pass the result of that method to the static
FromHandle method on the Control class and it will return the Control
instance that is associated with that handle (assuming it is a .NET
control).

Hope this helps.
 
Back
Top