Determining the Active Control

  • Thread starter Thread starter Joe Hanna
  • Start date Start date
J

Joe Hanna

Hi Everyone,

Just wondering how you can determine the Active Control on a form in the absence of the ActiveControl Member from .NET CF Forms?

Tried Google, but there was nothing on .NET CF.

Thanks in advance,
Joe
 
You're going to have to loop through the controls, starting with the main
Form, checking the Focused property to see which control has focus. Adding
the following property and helper method to your Form should do what you
want:

public virtual Control ActiveControl
{
get
{
return GetFocusedControl(this);
}
set
{
if (!value.Focused)
{
value.Focus();
}
}
}

private Control GetFocusedControl(Control parent)
{
if (parent.Focused)
{
return parent;
}
foreach (Control ctrl in parent.Controls)
{
Control temp = GetFocusedControl(ctrl);
if (temp != null)
{
return temp;
}
}
return null;
}

--
Tim Wilson
..Net Compact Framework MVP

Hi Everyone,

Just wondering how you can determine the Active Control on a form in the
absence of the ActiveControl Member from .NET CF Forms?

Tried Google, but there was nothing on .NET CF.

Thanks in advance,
Joe
 
Back
Top