Skip to content


Finding currently focused control

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Good question!  Now, this was a question in one of my sessions at Microsoft Community at Cognizant.  We need to use User32 API’s to do this stuff. It isn’t that difficult. Just a small snippet and you are through!

Here’s the neat solution.

[DllImport("user32.dll", CharSet=CharSet.Auto, CallingConvention=CallingConvention.Winapi)]
internal static extern IntPtr GetFocus();

private Control GetFocusedControl()
{
          Control focusedControl = null;

          // To get hold of the focused control:
          IntPtr focusedHandle = GetFocus();

          if(focusedHandle != IntPtr.Zero){

                 // Note that if the focused Control is not a .Net control, then this will return null.
                focusedControl = Control.FromHandle(focusedHandle);
         }
         return focusedControl;
}


Related Posts:

Posted in C#, Microsoft .NET, Winform.

Tagged with , .


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.