Hello everyone,
I need to find the total number of root nodes in a treeview. This is not including child nodes.
Treeview1.Nodes.Count gives me all total nodes.
Thank You in Advance.
Printable View
Hello everyone,
I need to find the total number of root nodes in a treeview. This is not including child nodes.
Treeview1.Nodes.Count gives me all total nodes.
Thank You in Advance.
There is only one Root Node (The First one Added), but if you mean all the Top Level Nodes (Those that have no Parent) then Enumerate the Nodes collection comparing the Node Text to the FullPath property, if the Node is at the TopLevel both properties would be the same, i.e.Code:Private Sub Command1_Click()
Dim oNode As Node
For Each oNode In TreeView1.Nodes
If oNode = oNode.FullPath Then List1.AddItem oNode.Text
Next
End Sub
Thanks again Aaron. That did the trick. What if I want to count child nodes?
This one shouldn't waste too much time on checking all child nodes
Code:Private Sub Form_Click()
With TreeView1.nodes
Set temp = .Add(, , , "A")
.Add temp, tvwChild, , "AB"
.Add , , , "B"
End With
For Each n In RootNodes(TreeView1.nodes)
Debug.Print n.Text
Next n
End Sub
Function RootNodes(nodes As nodes) As Node()
Dim temp As Node, roots() As Node, n As Long
If nodes.Count Then
ReDim roots(0)
Set temp = nodes.Item(1).Root
Set roots(0) = temp
Set temp = temp.Next
Do Until temp Is Nothing
n = n + 1
ReDim Preserve roots(n)
Set roots(n) = temp
Set temp = temp.Next
Loop
End If
RootNodes = roots
End Function
BTW, that adds them into an array, not count them, althogh easy to fix, to get the amount of direct childs under a node theres children property
Code:msgbox node.children
Thank You Kedaman.
The Children property combined with Aaron's code will return what I want.
Thank You to both!
Have a good day!!!
:):)