To trace the parent control
I want to trace all the parent controls of a control (e.g) if i have a form which contains 开发者_StackOverflow社区panel which in turn contains another panel which contains button, i want to trace all the parents(form,panel,panel) from button how to do that?
You can try something like
private List<Control> FindParentList(Control control)
{
List<Control> retVal = new List<Control>();
Control c = control;
while (c.Parent != null)
{
Control parent = c.Parent;
retVal.Add(parent);
c = parent;
}
return retVal;
}
And to call the method use
List<Control> parentList = FindParentList(button1);
The property Parent (inherited on every control from Control.Parent) provides access to the parent control. One can follow these references all the way up to the top-level Form.
精彩评论