Solved

How to get Child nodes in treeview on lowes level

Posted on 2011-09-08
5
1,777 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
LINQ - C# to VB convertion 12 56
Help with Query not working in client's PC 1 36
Change Format on Data when exporting to Excel 4 32
Get Client IP on RDS - VB.NET 15 44
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
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.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

786 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