Results 1 to 6 of 6

Thread: Root Count?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    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.
    Chemically Formulated As:
    Dr. Nitro

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    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

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    Thanks again Aaron. That did the trick. What if I want to count child nodes?
    Chemically Formulated As:
    Dr. Nitro

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    Thank You Kedaman.

    The Children property combined with Aaron's code will return what I want.

    Thank You to both!

    Have a good day!!!

    Chemically Formulated As:
    Dr. Nitro

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width