Click here to Skip to main content
13,259,138 members (47,460 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 19 Feb 2013

How to Display a Control's Hierarchy as a String

, 26 Feb 2013
Rate this:
Please Sign up or sign in to vote.
Here's how to display a control's hierarchy as a string

I created an extension method that allows my program to find all of a control's parents' names. It returns a string separated overrideable delimiter (::).  This code is targeted for .NET 4 Client, but should also work in .NET 3.5.

Module WinformControlExtensions
  ''' <summary>
  ''' Find all of the control's parents' names and return a string of the complete hierarchy
  ''' </summary>
  ''' <param name="Control"><c><seealso cref="System.Windows.Forms.Control">Control</seealso></c> 
  ''' from which to display the hierarchy.</param>
  ''' <returns><c><seealso cref="String">String</seealso></c> of the parents' names.</returns>
  ''' <remarks></remarks>
  Public Function ToControlHierarchy(ByRef Control As Control, _
                                     Optional ByVal Separator As String = "::") As String
    Dim oStack As New Stack()

    Dim oParent As Control = Control.Parent
    Do Until IsNothing(oParent)
      oParent = oParent.Parent

    Dim sFullName As String = String.Empty
    Do While oStack.Count > 0
      sFullName &= oStack.Pop() & Separator

    Return sFullName.Substring(0, sFullName.Length - Separator.Length)
  End Function
End Module

An example usage:

Dim oList As List(Of Control) = Me.FindAllChildren()

Dim oControl As Control
For Each oControl In oList


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


About the Author

Adam Zuckerman
Software Developer (Senior)
United States United States
Long time software engineer who rambles occasionally about coding, best practices, and other random things.

You may also be interested in...


Comments and Discussions

SuggestionSmall optimization Pin
Pikoslav28-Feb-13 4:28
memberPikoslav28-Feb-13 4:28 

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
Web03 | 2.8.171114.1 | Last Updated 27 Feb 2013
Article Copyright 2013 by Adam Zuckerman
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid