Storing items in a TreeView control is one of the basic operations you can perform with the control. But how do you retrieve the nodes of the tree? The simplest method is to scan the Nodes collection, which contains all the nodes of the tree. The most appropriate loop for scanning a collection is the For Each … Next loop, which is shown next:
Dim node As Node’
For Each node In TreeViewl.Nodes
The node variable represents a different node at each iteration. To process the current node, use the node variable’s members. The following statement will print the node’s text on the Immediate window:
Debug.Print node. Text
The Scan Nodes button prints all the nodes in the order they were entered. Its Click event handler is shown next:
Dim node As Node
For Each node In TreeViewl.Nodes
Debug.Print node. Text
This loop enumerates the Nodes collection of the control, and. the variable node returns the next node at each iteration. The following strings will appear in the Immediate window:
The node names will appear in the order they were entered without any indication of their structure. A better technique for scanning a tree and enumerating its items will be discussed later on in the section “Scanning the Tree’View Control.” In the next section, we’re going to put together all the information presented so far to build a “real” application based on the TreeView control.
VB6 at Work: The Globe Project
The Globe project, which you can find in this chapter’s folder on the CD, demonstrates many of the techniques we’ve discussed so far. It’s not the simplest example of a TreeView control, and its code is lengthy, but it will help you understand how to manipulate nodes atnmtime. As you know by now, Treeview is not a simple control, so I would like to end this section with an advanced example that you can use as a starting point for your own custom applications. Yow’ll also see how to save the nodes of a TreeView control to a disk file and retrieve l’ em later, a basic operation that can.’t be performed with a method call (as one.might expect).
The Globe project contains a single Form, which is shown. The TreeView control at the left contains a tree structure with continents, countries, and cities, with a rather obvious structure. Each city belongs to a country and each country belongs to a continent. The control is populated at runtime from within the Form’s Load event. When a node is selected in the TreeView control, its text is displayed on the TextBox controls at the bottom of the Form. When a continent name is selected, the continent’s name appears in the firs TextBox and the other two Textboxes are empty. When a country is selected, its name appears in the second TextBox and its continent appears in the first TextBox. Finally, when a city is selected, it appears in the third TextBox, along with its country and continent in the other two TextBoxes.
You can also use the TextBox controls to add new nodes. To add a new continent, just supply the name of the continent in the first TextBox. To add a new country, supply its name in the second TextBox and the name of the continent it belongs to in the first one. Finally, to add a city, supply a continent, country, and city name in the three TextBoxes.
Run the Globe application and expand the continents and countries to see the tree structure of the data stored in the control. Add new nodes to the control. and enumerate its nodes by clicking the appropriate button on the right-hand side of the Form.These buttons list the nodes at a given level (continents, countries and cities). When you add new nodes, the code places them in their proper place in the list. If you specify a new city and a new country, then a new country node will be created under the specified continent and a new city node will be inserted under the specified country. Or, you can add a new country. node and then a city node. Just remember that country names and city names must be unique throughout the tree structure, not only in the continent or country to which they belong.
Coding the Globe Project
Let’s take a look at how the Globe project is coded. We’ll start by looking at the code that populates the TreeView control. First, the root node (Globe) is added. This is the top-level node and all other nodes are children of this node:
Notice that the reference to the newly created node returned by the Add method is stored to the Nd variable. This node’s tag is set to “GLOBE” and its Sorted property is set to True. This means that the continent names placed directly under the Globe node will be automatically sorted.
Then, the code adds the continents directly under the Globe node. The continents are child nodes of the node whose key is “GLOBE”. This is done using the following statements (I’m only showing the statements for adding the first two continents; the remaining continents are added with similar statements):
After the continents are in place, the code adds the countries to each continent, and cities to each country. Here are the statements that .a,dd the “Germany” node:
Germany is placed under the node with the key “EUROPE”, its Tag property is set to “Countries”, and its Sorted property is set to True. This means that all German cities will be sorted automatically. Finally, the following statements insert city nodes under Germany:
Notice that you don’t have to set the Sorted property of the city nodes, because they don’t have child nodes. The cities will appear sorted because the Sorted property of the country node they belong to has its Sorted property set to True. As you can see, populating the TreeView control from within the application’s code is ‘ straightforward. As long as you assign meaningful keys to the items and add them in their natural order, you can create very elaborate tree structures. You can create long trees with many branches, which you couldn’t possibly draw on paper. Yet, with the navigational tools built into the control, it’s still easy to traverse them and locate the information you need.
The keys of the nodes are the same as their text (keys are in uppercase to simplify searching). That’s why you can’t have two cities with the same name, even if they belong to different countries.
The following code segment adds Africa and all its child nodes to the Globe node.
It shows a segment of a TreeView control and the commands for adding a few of the nodes. The text above the statements shows how to read the arguments of the Add method (that’s the trick to understanding where each node belongs). The third argument, KClJ, has a relationship to the first argument, Relative, which is specified by the second argument. Re. “‘,,nship. The tcp statement reads “America is a child of Globe”; the next statement reads “Canada is a child of America,” and so on.
Retrieving the Selected Node
The expression Nd. Parent returns the Nd node’s parent node (which is the continent). This node’s Text property is assigned to the Text2 control. Finally, with the same statement, it extracts the country’s parent node and displays its Text property in the Text! control.If the selected node’s.Tag property is”Countries”, the code leaves the lasttextbox blank and displays only the country and continent names in the other two textboxes, using similar statements.
The code behind Delete Current Node and Expand Current Node is simple. To delete a node, call the node’s Remove method, passing the node’s index as argument.’ The index of the selected node is given by the property TreeViewl.Selected Item .Index. In addition, the event handler must also clear the three TextBox controls. Here’s the Delete button’s code:
Private Sub DeleteNodeBttn_Click()
Textl.Text = ” ”
Text2. Text = ” ”
Text3.Text = ” ”
End Sub= ” ”
The other button expands the current node by setting its Expanded property to True:
Private Sub ExpandNodeBttn_Click()
TreeV;ewl.Selectedltem.Expanded = True