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

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);
}
Was this snippet helpful for you? YESYES / NONO
Send us feedback about this snippet »



Related Snippets: