How Determine Which Control has Focus? #2

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.
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top