private List<control> descendantControls = new List<control>();
protectedoverridevoid OnControlAdded(ControlEventArgs e)
{
AppendControl(e.Control);
base.OnControlAdded(e);
}
protectedoverridevoid OnControlRemoved(ControlEventArgs e)
{
RemoveControl(e.Control);
base.OnControlRemoved(e);
}
privatevoid AppendControl(Control c)
{
c.LostFocus += new EventHandler(child_LostFocus);
descendantControls.Add(c);
foreach (Control child in c.Controls)
{
AppendControl(child);
}
}
privatevoid RemoveControl(Control c)
{
c.LostFocus -= new EventHandler(child_LostFocus);
descendantControls.Remove(c);
foreach (Control child in c.Controls)
{
RemoveControl(child);
}
}
privatevoid child_LostFocus(object sender, EventArgs e)
{
if (this.Focused)
return;
foreach (Control child in descendantControls)
{
if (child.Focused)
{
return;
}
}
// If we get here non of our descendant controls have the focus so close.
Close();
}</control></control>