Click here to Skip to main content
13,298,655 members (48,808 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 8 Jul 2010

Find a Parent Control Recursively

, 8 Jul 2010
Rate this:
Please Sign up or sign in to vote.


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;
                    rControl = findControlParent(theControl.Parent);
                rControl = null;
            return rControl;


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


About the Author

United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralEnhanced for VB.NET Pin
jasonHall26-Mar-14 9:19
memberjasonHall26-Mar-14 9:19 
GeneralDoes the job well Pin
Tanner Wood11-Feb-14 5:24
memberTanner Wood11-Feb-14 5:24 
GeneralWorks nicely and quite quick and exactly what I needed Pin
Ren33321-Sep-10 20:11
memberRen33321-Sep-10 20:11 
GeneralReason for my vote of 4 very helpfull Pin
Koshy Panicker John8-Jul-10 21:15
memberKoshy Panicker John8-Jul-10 21: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 8 Jul 2010
Article Copyright 2010 by jasonHall
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid