Help about Treeview Control

popel

New member
Joined
Oct 1, 2009
Messages
3
Location
Bangladesh
Programming Experience
1-3
How can I Collect all the Checked Child Nodes Name From a TreeView Control Using VB.NET Windows Applications. Thanks In Advanced.

Popel
 
When you say "child nodes" do you mean specifically not root nodes, i.e. not the nodes at he top level but all others? If so, how deep does your tree go? Is there just one level of children below the root nodes or can there be more?
 
When you say "child nodes" do you mean specifically not root nodes, i.e. not the nodes at he top level but all others? If so, how deep does your tree go? Is there just one level of children below the root nodes or can there be more?
Yes..... u r right. There are more than 20 root nodes and every root nodes have more than 30 child node. Again few child nodes have one or two child. Specially i want to collect all the node name which are checked. Thanks For ur Reply. SORRY FOR MY POOR ENGLISH.
 
OK, so you want to ignore the root nodes and you want to get all the checked nodes from multiple levels of child nodes, correct? If so then this will do the job:
VB.NET:
Private Function GetCheckedChildNodes(ByVal nodes As TreeNodeCollection) As TreeNode()
    Dim checkedNodes As New List(Of TreeNode)

    For Each node As TreeNode In nodes
        If node.Checked AndAlso node.Level > 0 Then
            checkedNodes.Add(node)
        End If

        checkedNodes.AddRange(Me.GetCheckedChildNodes(node.Nodes))
    Next

    Return checkedNodes.ToArray()
End Function
Example usage:
VB.NET:
Private Sub Button1_Click() Handles Button1.Click
    For Each node In Me.GetCheckedChildNodes(Me.TreeView1.Nodes)
        Console.WriteLine(node.FullPath)
    Next
End Sub
 
Back
Top