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
Solved

How to get Child nodes in treeview on lowes level

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET 2008 Publish Error 2 34
Calculate number of nights between two dates 5 54
Looping through datagridview and dataset ? 6 24
VB.NET Inline If statement 4 15
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 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