Nodes Property Example

This example adds several Node objects to a TreeView control. When the form is clicked, a reference to each Node is used to display each Node object's text. 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 form.

Private Sub Form_Load()
   Dim nodX As Node
   Set nodX = TreeView1.Nodes.Add(,,"R","Root")
   Set nodX = TreeView1.Nodes.Add("R", tvwChild,"C1","Child 1")
   Set nodX = TreeView1.Nodes.Add("R", tvwChild,"C2","Child 2")
   Set nodX = TreeView1.Nodes.Add("R", tvwChild,"C3","Child 3")
   Set nodX = TreeView1.Nodes.Add("R", tvwChild,"C4","Child 4")
   nodX.EnsureVisible
   TreeView1.Style = tvwTreelinesText ' Style 4.
   TreeView1.BorderStyle = vbFixedSingle
End Sub

Private Sub Form_Click()
   Dim i As Integer
   Dim strNodes As String
   For i = 1 To TreeView1.Nodes.Count
   strNodes = strNodes & TreeView1.Nodes(i).Index & " " & _
   "Key: " & TreeView1.Nodes(i).Key & " " & _
   "Text: " & TreeView1.Nodes(i).Text & vbLF
   Next i
   MsgBox strNodes
End Sub