Root Property Example

This example adds several Node objects to a TreeView control. When you click a Node, the code navigates up the tree to the Root node, and displays the text of each Parent node. 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 a Node.

Private Sub Form_Load()
   Dim nodX As Node   ' Create a tree.
   Set nodX = TreeView1.Nodes.Add(,,"r", "Root")
   Set nodX = TreeView1.Nodes.Add(,,"p", "Parent")
   Set nodX = TreeView1.Nodes.Add("p",tvwChild,, "Child 1")
   nodX.EnsureVisible   ' Show all nodes.
   Set nodX = TreeView1.Nodes.Add("r",tvwChild,"C2", "Child 2")
   Set nodX = TreeView1.Nodes.Add("C2",tvwChild,"C3", "Child 3")
   Set nodX = TreeView1.Nodes.Add("C3",tvwChild,, "Child 4")
   Set nodX = TreeView1.Nodes.Add("C3",tvwChild,, "Child 5")
   nodX.EnsureVisible   ' Show all nodes.
End Sub

Private Sub TreeView1_NodeClick(ByVal Node As Node)
   Dim n As Integer
   Dim strParents As String    ' Variable for information.
   n = Node.Index   ' Set n to index of clicked node.
   strParents = Node.Text & vbLF 
   While n <> Node.Root.Index
      strParents = strParents & _
      TreeView1.Nodes(n).Parent.Text & vbLF
      ' Set n to index of next parent Node.
      n = TreeView1.Nodes(n).Parent.Index
   Wend
   MsgBox strParents
End Sub