Children Property Example

This example puts several Node objects in a TreeView control. The code checks to see if a Node has children nodes. If so, then it displays the text of the children nodes. To try the example, place a TreeView control on a form and paste the code into the form's Declarations section. Run the example, click a Node object to select it, then click the form to see the text of the Node object's children.

Option Explicit
Private Sub Form_Click()
   Dim strC As String 
   Dim N As Integer
   If TreeView1.SelectedItem.Children > 0 Then ' There are children.

      ' Get first child's text, and set N to its index value.
      strC = TreeView1.SelectedItem.Child.Text & vbLF 
      N = TreeView1.SelectedItem.Child.Index

      ' While N is not the index of the child node's
      ' last sibling, get next sibling's text.
      While N <> TreeView1.SelectedItem.Child.LastSibling.Index
         strC = strC & TreeView1.Nodes(N).Next.Text & vbLF
         ' Reset N to next sibling's index.
         N = TreeView1.Nodes(N).Next.Index
      Wend
      ' Show results.
      MsgBox "Children of " & TreeView1.SelectedItem.Text & _
      " are: " & vbLF & strC
   Else ' There are no children.
      MsgBox TreeView1.SelectedItem.Text & " has no children"
   End If
End Sub

Private Sub Form_Load()
   TreeView1.BorderStyle = 1  ' Ensure border is visible
   Dim nodX As Node
   Set nodX = TreeView1.Nodes.Add(,,"d","Dates")
   Set nodX = TreeView1.Nodes.Add("d",tvwChild,"d89","1989")
   Set nodX = TreeView1.Nodes.Add("d",tvwChild,"d90","1990")

   ' Create children of 1989 node.
   Set nodX = TreeView1.Nodes.Add("d89",tvwChild, ,"John")
   Set nodX = TreeView1.Nodes.Add("d89",tvwChild, ,"Brent")
   Set nodX = TreeView1.Nodes.Add("d89",tvwChild, ,"Eric")
   Set nodX = TreeView1.Nodes.Add("d89",tvwChild, ,"Ian")
   nodX.EnsureVisible ' Show all nodes.

   ' Create children of 1990 node.
   Set nodX = TreeView1.Nodes.Add("d90",tvwChild, ,"Randy")
   Set nodX = TreeView1.Nodes.Add("d90",tvwChild, ,"Ron")
   nodX.EnsureVisible ' Show all nodes.
End Sub