FirstSibling Property Example

This example adds several nodes to a TreeView control. The FirstSibling property, in conjunction with the Next property and the LastSibling property, is used to navigate through a clicked Node object's hierarchy . To try the example, place a TreeView control on a form and paste the code into the form's Declarations section. Run the example and click the various nodes to see what is returned.

Private Sub Form_Load()
   Dim nodX As Node
   Set nodX = TreeView1.Nodes.Add(,,"dad","Mike") ' A first sibling.
   Set nodX = TreeView1.Nodes.Add(,,"mom","Carol")
   Set nodX = TreeView1.Nodes.Add(,,,"Alice")
   
   ' Marsha is the FirstSibling.
   Set nodX = TreeView1.Nodes.Add("mom",tvwChild,,"Marsha")
   Set nodX = TreeView1.Nodes.Add("mom",tvwChild,,"Jan")
   Set nodX = TreeView1.Nodes.Add("mom",tvwChild,,"Cindy")
   nodX.EnsureVisible ' Show all nodes.

   ' Greg is the FirstSibling.
   Set nodX = TreeView1.Nodes.Add("dad",tvwChild,,"Greg")    
   Set nodX = TreeView1.Nodes.Add("dad",tvwChild,,"Peter")   
   Set nodX = TreeView1.Nodes.Add("dad",tvwChild,,"Bobby")
   nodX.EnsureVisible ' Show all nodes.
End Sub

Private Sub TreeView1_NodeClick(ByVal Node As Node)
   Dim strText As String
   Dim n As Integer
   ' Set n to FirstSibling's index.
   n = Node.FirstSibling.Index
   ' Place FirstSibling's text & linefeed in string variable.
   strText = Node.FirstSibling.Text & vbLF
   While n <> Node.LastSibling.Index
   ' While n is not the index of the last sibling, go to the
   ' next sibling and place its text into the string variable.
      strText = strText & TreeView1.Nodes(n).Next.Text & vbLF
   ' Set n to the next node's index.
      n = TreeView1.Nodes(n).Next.Index
   Wend
   MsgBox strText   ' Display results.
End Sub