This example adds several nodes to a TreeView control. The Previous property, in conjunction with the LastSibling 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(, , "r", "Root")
Set nodX = TreeView1.Nodes.Add(, , "p", "parent")
Set nodX = TreeView1.Nodes.Add("r", tvwChild, , "Child 1")
Set nodX = TreeView1.Nodes.Add("r", tvwChild, , "Child 2")
Set nodX = TreeView1.Nodes.Add("r", tvwChild, , "Child 3")
nodX.EnsureVisible ' Show all nodes.
Set nodX = TreeView1.Nodes.Add("p", tvwChild, , "Child 4")
Set nodX = TreeView1.Nodes.Add("p", tvwChild, , "Child 5")
Set nodX = TreeView1.Nodes.Add("p", tvwChild, , "Child 6")
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 LastSibling's index.
n = Node.LastSibling.Index
' Place LastSibling's text & linefeed in string variable.
strText = Node.LastSibling.Text & vbLF
While n <> Node.FirstSibling.Index
' While n is not the index of the FirstSibling, go to the
' previous sibling and place its text into the string variable.
strText = strText & TreeView1.Nodes(n).Previous.Text & vbLF
' Set n to the previous node's index.
n = TreeView1.Nodes(n).Previous.Index
Wend
MsgBox strText ' Display results.
End Sub