browse: forums | FAQ
Connecting Tech Pros Worldwide

Hey there! Do you need C# / C Sharp help?

Get answers from our community of C# / C Sharp experts on BYTES! It's free.

How Determine Which Control has Focus? #2

Liam McNamara
Guest
 
Posts: n/a
#1: Nov 15 '05
I am having the same problem as the one I found in google below:

http://groups.google.com/groups?hl=e...p02%26rnum%3D3

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.




Nicholas Paldino [.NET/C# MVP]
Guest
 
Posts: n/a
#2: Nov 15 '05

re: How Determine Which Control has Focus? #2


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.


--
- Nicholas Paldino [.NET/C# MVP]
- nicholas.paldino@exisconsulting.com

"Liam McNamara" <mail@newsgroup.net> wrote in message
news:3F1F9F8D.6070308@newsgroup.net...[color=blue]
> I am having the same problem as the one I found in google below:
>
>[/color]
http://groups.google.com/groups?hl=e...p02%26rnum%3D3[color=blue]
>
> 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.
>[/color]


Liam McNamara
Guest
 
Posts: n/a
#3: Nov 15 '05

re: How Determine Which Control has Focus? #2


Thanks Nicholas, exactly what I was looking for.


Nicholas Paldino [.NET/C# MVP] wrote:[color=blue]
> 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.
>
>[/color]

Closed Thread