This example populates a TreeView control with the titles of files in a ListBox control. When an item in the TreeView control is clicked, the Text property is displayed in a Label on the form. To try the example, place TreeView, Label, and ListBox controls on a form and paste the code into the form's Declarations section. Run the example and click on any item to see its Text property.
Private Sub Form_Load()
Dim nodX As Node ' Declare an object variable for the Node.
Dim i As Integer ' Declare a variable for use as a counter.
' Add one Node to the TreeView control, and call it the first node
Set nodX = TreeView1.Nodes.Add()
nodX.Text = "First Node"
'Populate the ListBox
List1.AddItem "Node1" ' Add each item to list.
List1.AddItem "Node2"
List1.AddItem "Node3"
List1.AddItem "Node4"
List1.AddItem "Node5"
List1.AddItem "Node6"
List1.AddItem "Node7"
' Add child nodes to the first Node object. Use the
' ListBox to populate the control.
For i = 0 To List1.ListCount - 1
Set nodX = TreeView1.Nodes.Add(1, tvwChild)
nodX.Text = List1.List(i)
Next i
Treeview1.Nodes(1).Selected = True
nodX.EnsureVisible ' Make sure the node is visible.
End Sub
Private Sub TreeView1_NodeClick(ByVal Node As Node)
' Display the clicked Node object's Text property.
Label1.Caption = Node.Text
End Sub