Solved

How to get Child nodes in treeview on lowes level

Posted on 2011-09-08
5
1,628 Views
Last Modified: 2012-08-13
Hi,
I populate treeView with hierarchy up to 5 levels...
How can i get child nodes on lowest level (in this example on 5 th level ) depending of selected Parent...
For instance, i want to populate list box with child nodes on lowest level ...
If I select e.g. Node on 2nd level in treeview my list box should be populated with all child nodes defined on 5th level which belong to selected parent...
How can I make this working....

Please help...
0
Comment
Question by:alsam
  • 3
  • 2
5 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Do you want nodes at the 5th level specifically?...or just any "leaf" nodes (nodes with no children) below the currently selected node?
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
Comment Utility
Here is how to find all "leaf" nodes:  
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not IsNothing(TreeView1.SelectedNode) Then
            Dim Values As New List(Of String)
            FindLeaves(TreeView1.SelectedNode, Values)
            ListBox1.DataSource = Values
        End If
    End Sub

    Private Sub FindLeaves(ByVal TN As TreeNode, ByVal leaves As List(Of String))
        For Each node As TreeNode In TN.Nodes
            If node.Nodes.Count = 0 Then
                leaves.Add(node.Text)
            Else
                FindLeaves(node, leaves)
            End If
        Next
    End Sub

Open in new window


Here is how to find all nodes at level 5:  
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not IsNothing(TreeView1.SelectedNode) Then
            Dim Values As New List(Of String)
            FindLeaves(TreeView1.SelectedNode, 4, Values) ' <-- Level is 0 (zero) based!
            ListBox1.DataSource = Values
        End If
    End Sub

    Private Sub FindLeaves(ByVal TN As TreeNode, ByVal Level As Integer, ByVal leaves As List(Of String))
        For Each node As TreeNode In TN.Nodes
            If node.Level = Level Then
                leaves.Add(node.Text)
            ElseIf node.Nodes.Count > 0 AndAlso node.Level < Level Then
                FindLeaves(node, Level, leaves)
            End If
        Next
    End Sub

Open in new window

0
 
LVL 1

Author Comment

by:alsam
Comment Utility
Hi,
the 5th level is just as example... its not fix category....Hierarchy levels can be different for diffrent hierarchies....
So basically I can have 5 or 6 or 7 etc. level hierarchy.... The last level (the lowest one) is what Im interestd in...
So basically, depending which parent node I select in hierarchy i would like to populate listbox with all children on lowest level...for ilustration let say I have 5 level hierarchy... when i select any node on 2nd level within the hierarchy list box should be populated with coresponding nodes from level 5....
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Try the first example then...  =)
0
 
LVL 1

Author Closing Comment

by:alsam
Comment Utility
Thank you very much...
First example produce the result as expected....
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

763 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

16 Experts available now in Live!

Get 1:1 Help Now