Solved

How to get Child nodes in treeview on lowes level

Posted on 2011-09-08
5
1,939 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 86

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 86

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 86

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

688 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