Click here to Skip to main content
15,892,575 members
Articles / Programming Languages / C#
Tip/Trick

Find a Parent Control Recursively

Rate me:
Please Sign up or sign in to vote.
4.71/5 (6 votes)
8 Jul 2010CPOL2 min read 38.5K   2   6

Introduction


When creating your own user controls, it is often necessary to register certain events from one of your user control's parent controls.  In most cases, you'd like to develop your custom user controls to be flexible enough to be used more than once, and often times by different types of parent controls.  Your user control may have n number of parent controls when all is said and done.  This example will allow you to recursively find a parent control of a specific type, if one exists.  The control I'm going to attempt to find is a TabControl.




Using the code

The method name that will perform the recursion is called findControlParent.  In your user control, you may call this method in many places; the Load event, the default constructor of your user control, or override the OnHandleCreated event.  Declare a control to accept the return value back from the findControlParent method and pass in your custom user control as the only parameter using the this.  Don't forget to include this:  using System.Windows.Forms;
Control theTabControl = findControlParent(this);
if (theTabControl != null)
{
   ((TabControl)theTabControl).Selecting +=newTabControlCancelEventHandler(handler_Selecting);
}
This is the findTabControlParent method.  This method takes your user control as a parameter and returns a Control.  The typeof() is used to determine if the parent control is a type of control specified.  In this example, I'm using the TabControl, but this could be any type of control.  The method calls itself recursively if the parent control fails the typeof() test and checks the parent's parent, and so forth.  This is done until the top parent node is found.  The top parent node will not have a parent, so the first if statement will return null, and the control returned is set to null.  In the calling method, we check for this before attempting to register the event (see code block above).    
private static Control findControlParent(Control theControl)
{
    Control rControl = null;

    if (theControl.Parent != null)
    {
        if (theControl.Parent.GetType() == typeof(System.Windows.Forms.TabControl))
        {
            rControl = theControl.Parent;
        }
        else

        {
            rControl = findControlParent(theControl.Parent);
        }
    }
    else
    {
        rControl = null;
    }
    return rControl;
}






License

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


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

Comments and Discussions

 
SuggestionGreat start, enhanced version Pin
Andrzej_kl25-Nov-21 6:40
Andrzej_kl25-Nov-21 6:40 
GeneralRe: Great start, enhanced version Pin
jasonHall22-Nov-22 7:21
jasonHall22-Nov-22 7:21 
GeneralEnhanced for VB.NET Pin
jasonHall26-Mar-14 8:19
jasonHall26-Mar-14 8:19 
GeneralDoes the job well Pin
Tanner Wood11-Feb-14 4:24
Tanner Wood11-Feb-14 4:24 
GeneralWorks nicely and quite quick and exactly what I needed Pin
RenatoK21-Sep-10 19:11
RenatoK21-Sep-10 19:11 
Works nicely and quite quick and exactly what I needed
GeneralReason for my vote of 4 very helpfull Pin
Koshy Panicker John8-Jul-10 20:15
Koshy Panicker John8-Jul-10 20:15 

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.