Click here to Skip to main content
15,891,033 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

Find a Parent Control Recursively

Rate me:
Please Sign up or sign in to vote.
4.67/5 (2 votes)
24 Sep 2010CPOL 19.3K   5   1
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:

C#
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;

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Austria Austria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 5 Automatic vote of 5 for accepting an... Pin
Chris Maunder29-Sep-10 16:38
cofounderChris Maunder29-Sep-10 16:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.