Control - TreeView  
How the TreeView control works
Adding Nodes at Runtime
Using the Tag Property
Sorting Nodes
Assigning Images to Nodes
Clearing a List
Removing Nodes
Counting List Entries
Getting Selected Node's Index
Searching a TreeView
Scrolling the List Programmatically

     How the TreeView control works  
A TreeView works much differently than a ListBox control.  With a TreeView, each item in the list is referred to as a Node, and the list is actually a collection of Nodes. So when you want to access any of the items in the TreeView control, you will have to access the Nodes Collection first. Hopefully the Examples below will clarify this for you...
     Adding Nodes at Runtime  
There are two ways of adding an item to a TreeView. You can simply run the regular .Add method:

 'for parent nodes 
  TreeView1.Nodes.Add , , "USERS", "Users" 
 
'for child nodes
 TreeView1.Nodes.Add "USERS", tvwChild, Name1, "Name 1"

 Or you can set  an object variable (the most common way and the most preferred) and gain quicker access to the newly added node for any extra tweaking without a lot of hassle.  

 Dim nItem As Node
 Set nItem = TreeView1.Nodes.Add , , "USERS", "Users" 

 The parameters for the Add method are pretty simple and straightforward. 

  .Add(relative, relationship, key, text, image, selectedimage)

Arguments  Description
relative*  The index number or key of a pre-existing Node object. Empty if parent node.
  
relationship*  Specifies the relative placement of the Node object. (See below) Empty if parent node.
  
key*  A unique string that can be used to retrieve the Node with the Item method.
  
text Required. The string that appears in the Node.
  
image*  The index of an image in an associated ImageList control.
  
selectedimage*  The index of an image in an associated ImageList control that is shown when the Node is selected.

*Optional

Constant Value Description
tvwFirst 0 First. The Node is placed before all other nodes at the same level of the node named in relative.
tvwLast 1 Last. The Node is placed after all other nodes at the same level of the node named in relative. Any Node added subsequently may be placed after one added as Last.
tvwNext 2 (Default) Next. The Node is placed after the node named in relative.
tvwPrevious 3 Previous. The Node is placed before the node named in relative.
tvwChild 4 Child. The Node becomes a child node of the node named in relative.

Although the Key is optional. I would recommend using it. This will make it easier for quicker access during searches and such. The key must be unique for each node. Usually I just use the same as the nodes text but in upper case. 


     Using the Tag Property  

Now if you wish to classify your nodes, then the Tag property can be used. For instance, you have a list of books. You want them all under the same branch but you want to specify which are hard-cover and which are soft-cover.  In this case you would use:

 Dim nItem As Node
 
'softcover
 Set nItem = TreeView1.Nodes.Add "BOOKS", tvwChild, Name2, "Name 2"
 nItem.Tag = "Softcover"
 'hardcover
 Set nItem = TreeView1.Nodes.Add "BOOKS", tvwChild, Name2, "Name 2"
 nItem.Tag = "Hardcover"

 

     Sorting Nodes  
If you wish all the child nodes under a parent to be sorted. Set the parent node's Sorted property to True.

 Dim nItem As Node
 Set nItem = TreeView1.Nodes.Add , , "USERS", "Users"
 nItem.Sorted = True 

     Assigning Images to Nodes  
If you want a little icon in your node you use the Image and the SelectedImage arguments of the Add method. (See 'Adding Nodes at Runtime') 

They will accept either a Picture Pointer, such as .Picture of a PictureBox or by using LoadPicture():

 TreeView.Nodes.Add  , , "USER", "User",LoadPicture ("C:\Pic.bmp")


Or you can associate an ImageList control to the TreeView control, and just use the Index of the image you wish to use.

 'add a picture to my ImageList
 ImageList1.ListImages.Add , , LoadPicture ("C:\Pic.bmp")
 ImageList1.ListImages.Add , , LoadPicture ("C:\Pic2.bmp")

 
'set the ImageList
 Treeview1.ImageList = ImageList1

 'add the node

 TreeView.Nodes.Add  , , "USER", "User", 1, 2
 

     Clearing a list  
To clear a TreeView, use the Clear method for the Nodes Collection:

 TreeView1.Nodes.Clear

     Removing Nodes  
Just use the collections Remove method. You just have to know the index of the item you want to remove.

TreeView1.Nodes.Remove (Index)

to remove selected nodes:

TreeView1.Nodes.Remove TreeView1.SelectedItem.Index 

 

     Counting list entries  

To count entries in a TreeView you can use the collections Count property :

  intCount = TreeView1.Nodes.Count

However, this will count all items whether they are a main category or a sub-category item. 

If you are wanting to know how many child nodes are under a parent node, then you can use the 'Children' property of the parent node.

  intCount = TreeView1.Nodes.Item(1).Children

This is assuming you know the index of the parent node.

And there is one other method of counting nodes. The GetVisibleCount method, which, obvious by its name, counts all visible nodes.

 intCount = TreeView1.GetVisibleCount 

 

     Getting Selected Node's Index  

 intIndex = TreeView1.SelectedItem.Index 

     Searching a TreeView  

There are different ways to do this. A simple way is using a regular For...Each loop. This will loop through each node in the TreeView and set nItem as an object variable to that node.

Dim nItem as Node

For Each nItem in TreeView1.Nodes
   
'process nItem
Next

'free it up
Set nItem = Nothing

However if you just want to know whether an entry is made already or not then you could use a function like this:

Private Function NameExists(ByVal Name As String) As Boolean
 Dim nNode As Node
 On Error Resume Next
 Set nNode = TreeView1.Nodes.Item(Name)
 If Err.Number = 0 Then
     NameExists = True
     Set nNode = Nothing
 End If
End Function

This method uses the 'key' property. So if you didn't use a key, this won't work.  It returns a boolean 'True' or 'False' depending on whether or not the node was found. You could alter it and have it return the nodes index instead if you wanted, like so:

Private Function NameExists(ByVal Name As String) As Integer
 Dim nNode As Node
 On Error Resume Next
 Set nNode = TreeView1.Nodes.Item(Name)
 If Err.Number = 0 Then
     NameExists = nNode.Index
     Set nNode = Nothing
 Else
     NameExists = -1
 End If
End Function

This would return the index if found, or -1 if not found.

     Scrolling the List Programmatically  
When you add nodes and it scrolls to the end of the list, you can reset it to the beginning of the list (or anywhere you want really), by using the Selected property:

 TreeView1.Nodes(1).Selected = True