Visual Basic: Windows Controls

Sorted Property (TreeView Control) Example

This example adds several Node objects to a tree. When you click a Node, you are asked if you want to sort the 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 to sort it.

  Private Sub Form_Load()
   ' Create a tree with several unsorted Node objects.
   Dim nodX As Node
   Set nodX = TreeView1.Nodes.Add(, , , "Adam")
   Set nodX = TreeView1.Nodes.Add(1, tvwChild, "z", "Zachariah")
   Set nodX = TreeView1.Nodes.Add(1, tvwChild, , "Noah")
   Set nodX = TreeView1.Nodes.Add(1, tvwChild, , "Abraham")
   Set nodX = TreeView1.Nodes.Add("z", tvwChild, , "Stan")
   Set nodX = TreeView1.Nodes.Add("z", tvwChild, , "Paul")
   Set nodX = TreeView1.Nodes.Add("z", tvwChild, "f", "Frances")
   Set nodX = TreeView1.Nodes.Add("f", tvwChild, , "Julie")
   Set nodX = TreeView1.Nodes.Add("f", tvwChild, "c", "Carol")
   Set nodX = TreeView1.Nodes.Add("f", tvwChild, , "Barry")
   Set nodX = TreeView1.Nodes.Add("c", tvwChild, , "Yale")
   Set nodX = TreeView1.Nodes.Add("c", tvwChild, , "Harvard")
   nodX.EnsureVisible
End Sub

Private Sub TreeView1_NodeClick(ByVal Node As Node)
   Dim answer As Integer
   ' Check if there are children nodes.   
   If Node.Children > 1 Then ' There are more than one children nodes.
      answer = MsgBox("Sort this node?", vbYesNo)  ' Prompt user.
      If answer = vbYes Then   ' User wants to sort.
         Node.Sorted = True
      End If
   End If
End Sub