TreeView Top Node name

Posted on 2009-04-29
Last Modified: 2012-05-06

I've a Treeview on a Winform, with this kind of structure:

Family 1
    Group 1
    Group 2
       Item 1
       Item 2
       Item 3

When I select Item 2, for example, I can get the name of the 2nd level node (in this case "Group 2") using trvMyTreeView.SelectedNode.Parent.Text. How can I get the name of the "family" (in this case "Family 1")?


Question by:jppinto
    LVL 69

    Expert Comment

    by:Éric Moreau
    can you show your code?
    LVL 12

    Expert Comment

    use a loop to get the parent for a particular level eg.
    Private Function GetParentNode(ByVal node As TreeNode, ByVal level As Integer) As TreeNode
        Dim tmp As TreeNode = node
        For n As Integer = 1 To level
            If (tmp.Parent Is Not Nothing) Then
                tmp = tmp.Parent
                ' Might want to do something here to say the level is too high?
                Return tmp
            End If
        Next n
        Return tmp
    End Function

    Open in new window

    LVL 48

    Accepted Solution


    You can do this:

    Dim result As String = Me.Treeview1.SelectedNode.FullPath.Substring(0, Me.Treeview1.SelectedNode.FullPath.IndexOf("\"c))
    LVL 33

    Author Closing Comment

    Thanks jpaulino. Simple & working fine!
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    I like both approaches...  =)

    Here is another one to get the "root" node using GuitarRich's approach:
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim tn As TreeNode = TreeView1.SelectedNode
            If Not IsNothing(tn) Then '
                Dim root As TreeNode = GetRootNode(tn)
                Debug.Print("Root: " & root.Text)
            End If
        End Sub
        Private Function GetRootNode(ByVal tn As TreeNode) As TreeNode
            While Not IsNothing(tn)
                If Not IsNothing(tn.Parent) Then
                    tn = tn.Parent
                    Return tn
                End If
            End While
            Return Nothing
        End Function
    End Class

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Suggested Solutions

    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now