How to get the form active control?

PabloG picture PabloG · May 28, 2010 · Viewed 30.6k times · Source

Is there a way to get the form active control?

I was checking in the help for the "Support.GetActiveControl" method but it isn't supported :) for the Compact Framework.

I suppose that I can track the focus by adding a custom GotFocus event handler to all the form controls, but I'm looking for a more generic way that can be implemented for any form.

Answer

Sherif Hassaneen picture Sherif Hassaneen · Dec 1, 2012

This example displays the name of the currently selected Windows Forms control in a Label control.

private void DisplayNameOfActiveControl()
{
    label1.Text = this.ActiveControl.Name;
}