Nodes can be added to the
TreeView control by using
the Add method of the Nodes Collection. The syntax for
Add
is as follows:
TreeView1.Nodes.Add(relative,
relationship, key, text, image, selectedimage)
All the arguments for the Add method are optional.
The Relative argument gives VB the Key or Index of an existing node, and the Relationship
parameter tells VB where to place the new node in relation to the relative node.
If the Relative and relationship arguments are not specified, the new node will
be placed at the top level in the tree after all other existing nodes at that
level.
The values for the relationship argument are as follows:
- tvwFirst (0) The new node
is placed at the same level in the tree as the
"relative." It will be added as
the first node of that level.
- tvwLast (1) The new node
is placed at the same level in the tree as the
"relative" but will be added after
the last existing node at the same level as
"relative."
- tvwNext (2) The new node
will be placed at the same level in the tree
as the "relative," immediately following
that node.
- tvwPrevious (3) The new
node will be placed at the same level in the
tree as the "relative," immediately
preceding that node.
- tvwChild (4) The new node
will be a child of the "relative"
node.
The Key property identifies the new node in the
tree. If provided as an argument, it must be a unique string, not used as a key
by any other node in the tree. The key is used to retrieve or to find this node
when the index is not known.
The last three arguments of the Add method define
the appearance of the new node. The text that appears next to a node in the
TreeView is specified by the Text argument, a string value. If you want
to have icons appear in the TreeView alongside the
Text, you must first have an ImageList control on your
form. When you set up a TreeView and define its properties
through the Property Pages dialog box, you can bind an ImageList to the
TreeView . Figure 4.5 shows an example of this.

FIGURE 4.5 Binding an ImageList to a TreeView
control.
To include an icon with the node text, you can use the Image argument. This
argument has an integer value that corresponds to the index of an image in the
bound ImageList control. The ImageList
has to be set up first so that the index values are available for use in
the TreeView control. If you want a node to have a
different icon displayed when that node is selected by the user, you can specify
a second icon with the SelectedImage argument. This
is also an integer argument identifying an image in the same
ImageList.
Treeview Control topics