6

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.

Ian
  • 30,182
  • 19
  • 69
  • 107
PabloG
  • 25,761
  • 10
  • 46
  • 59
  • 1
    possible duplicate of [Know who got the focus in a Lost Focus event](http://stackoverflow.com/questions/2899338/know-who-got-the-focus-in-a-lost-focus-event) – ctacke May 28 '10 at 14:47

2 Answers2

8

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

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

You can iterate over all the controls in the form and check which one is focused.

Example: Getting ActiveControl in Compact Framework

Zeemee
  • 10,486
  • 14
  • 51
  • 81
Itay Karo
  • 17,924
  • 4
  • 40
  • 58