Monday, April 22, 2013

How To get The Focused Control


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);
}
messageBox(focused.Name);