Solved

How to get Child nodes in treeview on lowes level

Posted on 2011-09-08
5
1,728 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
ID: 36503385
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
ID: 36503501
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
ID: 36505200
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
ID: 36505263
Try the first example then...  =)
0
 
LVL 1

Author Closing Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.

912 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

24 Experts available now in Live!

Get 1:1 Help Now