Adding Node’s at Runtime VB Help

The Add Node button lets the user add new nodes to the tree at runtime. The number and type of the node(s) added depends on the contents of the TextBox controls:

• If only the Textl control contains text, then a new continent will be added.
• If the first two TextBox controls contain text, then:
• If a continent-exists, a new country node is added under the specified continent. .
•. If a continent doesn’t exist, a new continent node is added, then a new country node is added under the continent’s node.
• If all three TextBox controls contain text, the program adds a continent node (if needed), then a country node under the continent node (if needed), and finally, a city node under the country node.

Obviously, you can omit a city, or a city and country, but you can’t omit a continent name. likewise, you can’t specify a city without a country or a country without . a continent. The code will prompt you accordingly when it detects a condition that” prevents it from adding the new node for any reason. If the node exists already, then the program selects the existing node and doesn’t issue any warnings. The Add Node button’s code is shown next.

The On Error Resume Next statement tells Visual Basic to continue the execution of the code with the following statement, should it detect an error:

Set newNode = TreeV;ewl.Nodes.Item(UCaseS(Textl.Text))

The entire subroutine is based on this statement. The code will attempt to access a node to find out if it exists. The statement retrieves the node that corresponds to the continent specified in the first TextBox control and assigns the node retrieved to the new Node variable. However, if such a node doesn’t exist, a runtime error will be generated. Without the On Error statement, the application would end here with an error message.

Because of the On Error statement, the code continues with the following statement that examines the built-in Error variable:

If Error is True, the program knows it must add a continent node because it doesn’t exist. The same process is repeated for the country and city nodes, The program attempts ‘to retrieve the nodes that correspond to the keys entered by the user in the TextBox controls. If they don’t exist, they are added automatically.

As you can see, adding new nodes at runtime isn’t really complicated, but you must make sure the node you’re about to add doesn’t already exist. The Globe application uses the node’s Key value to determine whether a node exists or not. Each node’s key must be unique, and this raises some problems. For example, how can you add two identically named cities in different countries? The following section describes how to handle this situation.

VB6 at Work: The Revised Globe Project

To add identically named cities under different countries, you must modify the key so that it still identifies the node uniquely. The obvious choice is to prefix the keys of the cities with the names of the countries and continents to which they belong, for example, “Europe-Italy-Venice/and “America-United States-Venice”. You’d then be able to add a Venice node under both countries.

This technique comes with a price-it prevents you from locating a city by name instantly. For example, you’d no longer be able to search for “Venice”. You would need to know which Venice you were looking for and supply the entire key: “Europe- Italy-Venice” or “America-United States-Veruce”.

In the MEntries folder under the Globe folder on the CD, you’ll find a different version of the Globe project, which uses long keys made up of the continent, country, and city name. The revised Globe project’s code is very similar to the original code, except for the following:

1. The statements that populate the control prefix the old key with the keys of the parent nodes. The statements that add the nodes for Germany and German cities are:

Germany’s key is no longer “GERMANY”, but “EUROPE-GERMANY”, and Berlin’s key is n? longer “BERLIN”, b’;1t”EUROPE-GERMANY-BERLlN”.

2. The code that adds new nodes at runtime is also modified to use long keys. The following statements a~d a new country node:

Notice that it prefixes the country’s key by the continent’s key. Likewise, the following statements add a city node:

3. Finally, the Find button’s code expects that a full key will be supplied. In other words, it won’t locate items like “China” or “Kyoto”. Instead, you must provide the full key such as “Asia-China” or “Asia-Iapan-K yoto”. Open the project in the Visual Basic IDE and examine the differences in the code that searches the Treeview control for a key.

Listing Selected Nodes

The three buttons List Continents, List Countries, and List Cities, populate the List- Box control with”the names of the continents, countries, and cities, respectively. The code is straightforward and it’s based on the Next method of the Node object, which .returns the next node on the same level. The List Continents button, for example, retrieves the first child of the “GLOBE” node, which is the first continent name, with the statement:

Set Nd – TreeViewl.Nodes.ltem(‘GLOBE’)
Set childNd – Nd.Child

The variable childNd represents the first continent node. To retrieve the remaining ones, you must call the childNd object variable’s Next method as many times as there are continents in the tree. Each time, the Next method will return the next node on the same level. Here’s the complete listing of the List Continents button.

When the continentNd. Next method is called, it returns the next node in the Coninents level. Then the continentNd. children method is called and it returns the . first node in the Countries level. As you can guess, the code of the List Cities button uses the same two nested lists as the previous listing and an added inner loop, which scans the cities of each country.

The code behind these Command buttons requires some knowledge of the information stored in the tree. It will work with trees that have two or three levels of nodes like.the Globe tree, but what if the tree’s depth is allowed to grow to a dozen levels? A tree that-represents the structure of a folder on your hard disk, for example, may easily contain half a dozen nested folders. Obviously, to scan the nodes of this tree you can’t put together unlimited nested loops. The next section describes a technique for scanning any tree, regardless of how many levels it contains. the code in , the following section uses recursion.

Scanning the TreeView Control

The items of a TreeView control can all be accessed through the Nodes collection. You have seen how to scan the entire tree of the TreeView control with a For…Next loop. This technique, however, doesn’t reflect the structure of the tree; it simply lists all the nodes. The proper method to scan a tree is to exhaust each node, including its child nodes, before moving to the next node on the same level, as if you were scanning a folder. Displaying all the files in a folder along With the files in its subfolders isn’t very practical. We try to organize files under their folder and folders under their parent folders. We’ll do the ‘same with the TreeView control.

VB6 at Work: The TreeViewScan Project

To demonstrate the process of scanning a TreeView control, I have included the Tree-ViewScan project on the CD (you’ll find it in the TVWScan folder). The application’s Form is shown. The Form contains a TreeView control on the left, which is populated with the same data as the Globe’s TreeView control, and a ListBox control on the right, where the tree’s nodes are listed. Child nodes on the. ListBox control are indented according to the level of the tree to which they belong.

Scanning the child nodes in a tree calls for a recursive procedure, or a procedure that calls itself. Think of a tree structure that contains all the files and folders on your C: drive. If this structure contained no sub folders, you’d need to set up a loop to scan each folder, one after the other. Since most folders contain sub folders, the process must be interrupted at each folder to scan the subfolders of the current folder. The process of scanning a drive recursively is described in detail, Recursive Programming. Here, I will present the code to scan a tree and explain it a little. ~ you can’t follow the example of this section, you should read the discussion of the DirMap project.

Posted on November 5, 2015 in Advanced ActiveX Controls

Share the Story

Back to Top
Share This