How Determine Which Control has Focus? #2


Liam McNamara

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

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?




Nicholas Paldino [.NET/C# MVP]


You can import the GetFocus API method like this:

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

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