This example adds several Node objects to a TreeView control. The LastSibling property, in conjunction with the Next property and the FirstSibling property, is used to navigate through a clicked Node object's hierarchy level. 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")
Set nodX = TreeView1.Nodes.Add(,,"mom","Carol")
' Alice is the LastSibling.
Set nodX = TreeView1.Nodes.Add(,,,"Alice")
Set nodX = TreeView1.Nodes.Add("mom",tvwChild,,"Marsha")
Set nodX = TreeView1.Nodes.Add("mom",tvwChild,,"Jan")
' Cindy is the LastSibling.
Set nodX = TreeView1.Nodes.Add("mom",tvwChild,,"Cindy")
nodX.EnsureVisible ' Show all nodes.
Set nodX = TreeView1.Nodes.Add("dad",tvwChild,,"Greg")
Set nodX = TreeView1.Nodes.Add("dad",tvwChild,,"Peter")
' Bobby is the LastSibling.
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 is not the index of the last sibling, go to the
' next sibling and place its text into the string variable.
While n <> Node.LastSibling.Index
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