Find a Parent Control Recursively






4.67/5 (2 votes)
Useful function, but I don't see any need for an recursion here. I'd solve it like this:private static Control FindControlParent(Control control, Type type) { Control ctrlParent = control; while((ctrlParent = ctrlParent.Parent) != null) { ...
Useful function, but I don't see any need for an recursion here. I'd solve it like this:
private static Control FindControlParent(Control control, Type type)
{
Control ctrlParent = control;
while((ctrlParent = ctrlParent.Parent) != null)
{
if(ctrlParent.GetType() == type)
{
return ctrlParent;
}
}
return null;
}
and use it like this:
TabControl tc = FindControlParent(control, typeof(TabControl)) as TabControl;