65.9K
CodeProject is changing. Read more.
Home

Find a Parent Control Recursively

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.67/5 (2 votes)

Sep 24, 2010

CPOL
viewsIcon

19531

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;