How to

get focused control

Published: 27. January 2009 | Updated: 9. October 2011
License: Microsoft Public License (MS-PL)
Categories: Windows » Win32, Winforms
Tags: C# Controls Win32 Winforms
Was this snippet helpful for you? YESYES / NONO
Get Analyzant!

Check your entire website from code to content in minutes with Analyzant, the free analysis suite.

Free download at analyzant.com

Import namespaces

using System.Runtime.InteropServices;
using System.Windows.Forms;

Win API

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

Use

Control focused = null;
IntPtr handle = GetFocus();
if (handle != IntPtr.Zero)
{
    focused = Control.FromHandle(handle);
}
Send us feedback about this snippet »



Related Snippets: