K.Sopheak
K.Sopheak

Reputation: 23144

How to count and get child items of the last level of tree view node in vb net?

I want to count and get child name of treeview if there is only on root element like the following picture. I am in Item1 position.

 Item1
    |_____SubItem1
                 |___A
                 |___B

The result should be Count:2, Item: A, B

Upvotes: 1

Views: 5030

Answers (3)

Arifur Rahman
Arifur Rahman

Reputation: 1

**'Its work for me. Thanks** 




 Dim outputText As String


        For Each nd As TreeNode In TreeView1.Nodes
            If nd.Nodes.Count > 0 Then 'it has children, lets look at them
                For Each ndChild As TreeNode In nd.Nodes
                    outputText = String.Concat(ndChild.Text, " - (Total : ", ndChild.Nodes.Count, ")")
                    TextBox4.Text += outputText & vbNewLine

                    If ndChild.Nodes.Count > 0 Then 'it has children, lets look at them
                        For Each ndSubChild As TreeNode In ndChild.Nodes
                            outputText = String.Concat(vbTab, " - ", ndSubChild.Text)
                            TextBox4.Text += outputText & vbNewLine

                            If ndSubChild.Nodes.Count > 0 Then 'it has children, lets look at them
                                For Each ndSubSubChild As TreeNode In ndSubChild.Nodes
                                    outputText = String.Concat(vbTab, vbTab, " - ", ndSubSubChild.Text)
                                    TextBox4.Text += outputText & vbNewLine
                                Next
                            End If
                        Next
                    End If
                Next
            End If
        Next

Upvotes: 0

schroeder
schroeder

Reputation: 153

Also a possible solution, using LINQ:

Dim result = (From node as TreeNode in TreeView1.TopNode.Nodes
              Select New With {
                .Count = node.Nodes.Count,
                .Items = String.Join(",", node.Nodes.Cast(Of TreeNode).AsEnumerable().Select(Function(childNode) childNode.Name).ToArray())
              })

Upvotes: 1

Andrew Mortimer
Andrew Mortimer

Reputation: 2370

This solution relies on your TreeView depth not changing. If you're going to have deeper levels, you'll need to smarten it up.

       For Each nd As TreeNode In TestTreeView.Nodes
            If nd.Nodes.Count > 0 Then 'it has children, lets look at them
                For Each ndChild As TreeNode In nd.Nodes
                    If ndChild.Nodes.Count > 0 Then 'it has children, lets look at them
                        Dim outputText As String = String.Concat(ndChild.Text, " ",
                                                                 ndChild.Nodes.Count)
                        For Each ndSubChild As TreeNode In ndChild.Nodes
                            outputText = String.Concat(outputText, " ", ndSubChild.Text)
                        Next
                        Debug.Print(outputText)
                    End If
                Next
            End If
        Next

Upvotes: 2

Related Questions