| Control - TreeView |
| 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 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 The parameters for the Add method are pretty simple and straightforward. .Add(relative, relationship, key, text, image, selectedimage)
*Optional
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
|
|
| 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 |
|
| 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") 'add
a picture to my ImageList |
|
| 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:
|
|
| 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 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 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 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 |
|