Recursive Scanning

To start the scan of the TreeViewl control, start at the top node of the control with the statement:

ScanNode TreeViewl.Nodes(1)

The ScanNode() subroutine scans the nodes under the node that is passed as argument. By passing the root node to the ScanNode() subroutine, we’re in effect asking it to scan the entire tree. Let’s look now at the ScanNode() subroutine.

This subroutine is deceptively simple. First, it adds the name of the current node to the Listl ListBox control. If this node (represented by the a Node variable) contains child nodes, the code must scan them all. The aNode.Child method returns the first child node of a Node. After this method is called, the aNode.Next method returns the next child of the aNode node. Each time the aNode.Next is called, it returns a reference to the next node in the tree.

So, after we retrieve the first child node, we need to set up a loop, which must scan the child nodes of the current node, one at a time. To do this, the ScanNode() subroutine must call itself by passing a different argument. After the child nodes have been scanned, the code moves to the next node under ablode. if you’re familiar with recursive procedures, you’ll find the code quite simple. If not, this coding probably will raise many questions. You can use the ScanNode() subroutine as is to scan any TreeView control. All you need is a reference to the root node (or the node you want to scan recursively), which you must pass to the ScanNode() subroutine as argument. The subroutine will scan the entire tree and display its nodes on a ListBox control. The nodes will be printed one after the other. To make the list easier to read, indent the names of the nodes by an amount that’s proportional to the levels of nesting.  Nodes of the first level aren’t indented at all. Nodes on the first level can.be indented by 10 spaces, nodes on the second level can be indented by 20 spaces.and so on. The variable lWepfh keeps track of the level of nesting and is used to specify the indentation of the corresponding {lode. The revised ScanNode() sub- .. routine follows.

The RDepth variable is declared as Static because it must maintain its,value between calls. When we run into a node that has child nodes, we increase the RDeptll variable by one level. After we have scanned all the child nodes, we decrease it by one level.

RWl the lVWScan project and click the Expand Tree button to see all the nodes of the tree. Then click the Scan Tree button to populate the list on the right with the names of the continents/countries/ cities. Obviously, the ListBox control is not a substitute for the TreeView control. The data have no particular structure; even when they’re indented there are no tree lines on front of its nodes, and users pm’t expand and collapse the control’s contents, So why bother to map the contents of the Tree-View control to a ListBox control? The goal was to demonstrate how tll scan a tree structure and extract all the nodes along with their structure. You can use the Scan- ‘. Code() subroutine to store the nodes of a TreeView control to a disk file or transfer them to a database or another control. The ScanNode() subroutine is the core of the subroutine you need and can be adjusted to accommodate all the operations just – mentioned.

The Globe project has a small menu, which contains the Save and Load commands. Although these commands could have been implemented with a code similar to the ScanCode(), I used a non-recursive procedure. The Save command uses three nested . loops, which scan each continent, each country within each continent, and each city within each country. Each level’s nodes are prefixed by a space, so that the code that . ~ reads the data can easily figure out whether a specific node is ~child of another node. ‘ . The actual code Is-based on the code discussed in the section.’Enumerating Nodes.” Here’s the code b~hind the Save command of the Pile menu;

The Print statements are inserted to help you visualize the structure of the bee as it’s , , saved in a disk file. Part of the output produced by the Print statements is shown next:

·EUROPE·
.France
lyon’
Nice
Paris
Gennany.
Bayreuth·
Berlin
Frankfurt
Munich
Italy
Florencft
Milan
:Napoli
Rome
Venice.

You can .also open the text file created by the Save command with a text editor . . The Load ~d’s code reads one item at a time and inserts it in the TreeView . control in ~ proper order. The program examines the number of spaces in front of the node’s text If there’s a single space, the current node is a country and it’s added. under the continent that was added last to the tree. If there are two spaces, the current node is a city and it,’s added under the most recently added country node. The Load command’s code foHows.

The code of the Save and Load commands is lengthy but straightforward. A recursive procedure would have been shorter, but not as easy to follow for readers who aren’t familiar with recursive programming techniques.Recursive Programming, you should try to implement these two subroutines recursively as an exercise.

Using the ListView Control

The ListView control is similar to the ListBox control only ft can display its items in many forms along with any number of sub items for each item. To do the same with the’Listbox control. we had to make use of its ltemData property to store a pointer to the remaining items. With the ListVie.w control, you can store all the subitems along with the key item.

The basic properties of the ListView control can be set through the control’s property pages. Place an instance of the ListView control on a Form, right-click the control, and select Properties. The property pages shown will appear on your screen.

You can set up most of the control’s properties using the property pages, but you can’t populate the control. Soon you’ll see how to populate the Listview control through code, but let’s start with the basic properties:

General Tab This tab contains the basic properties that determine the control’s appearance and its operation.
• In the Mouse Pointer box you can specify the shape of the pointer, while it’s hovering over the ListView control.
• The Visual property determines how the items will be displayed on the control, and it can have one of the values shown in Table.
• The Arrange property determines how the items are arranged on the control, and it can have one of the values in Table.

Column Headers Tab If you plan to display the items in Report view, use this tab to set the headerS of.the columns. Use the Insert Column and Delete Column buttons to insert new columns or delete existing ones, respectively. Each column has a title (Text field) -and its own width (Width field). In addition to the. text of the column header, you can specify a key, tag, and icon for each column. To edit an existing column header, specify its index in the Index box.
Color Tab ·Usethis tab to specifythe control’s. background and foreground (text) colors.
Font Tab Use this tab to specify the font for the item labels.
Picture Tab Use this tab to specify a custom pointer and the image to appear on the control’s background.

Adding New Items

The items of the ListView control can be accessed through the Lisiltems property, which is a collection. As such, it exposes the standard members of a collection, which are:

Add method Adds a new item to the Lislltems collection
Count property’ Returns the number of items in the collection
Clear method Removes all the items from the collection
Item property Retrieves an item specified by an Index value or  key
Remove method . Removes an item from the Listlteins collection.

The Listltems collection’s methods have straightforward syntax, except for the Add method. To add a new item with the Add method, you must specify its index (the first item’s index is I), a key, its text, Lind two icons: a large one and a small .. one. Depending on the setting of the View property, one of these icons is displayed with each item. The syntax of the Add method is:

listViewl.Add Index, Key, Text, Icon, Smalllcon

The Add method returns a ListItem object, which represents the newly added item and has the following alternate syntax:

LItem – listView1.Add(Index, Key, Text, Icon, Small Icon)

You can use the Lltem variable in your code to manipulate the properties of the new item. Notice that none of the Add method’s arguments are mandatory. Of course, you can’t omit all the arguments. You can add an item specifying-only its icon and no text or a: Key value. Or, you can add an item with text only and omit its key or icon, if the application doesn’t allow the user to view the items as icons. Actually, the following statement will insert a new item in the list:

LItem· ListViewl ListItems.Add

but you must use the Litem variable to set some of its properties, as in the following statements:

.LItem.Text – ‘New Item’
LItem. Key – ‘New_Key.

The Index argument specifies the order of the new item in the control and it’s usually omitted. If you want to sort the items, use the Sorted property instead. The Key is a string that uniquely identifies an item, No two items in the ListView control can share the same Key value.If you attempt to insert an item with a key that exists already, ,1 trappable runtime error will be generated.

The last three arguments, Text, Icon, and Smalllcon, determine how the item will be displayed on the control. Which of the three values is used depends on the setting of the View property. If View – lvwIcon, then the Icon argument will specify that the large icon be display-ed above the item’s label.Jf the control’s View property is lvwUst or lvwReport, you must provide a value for the Text argument:

Scroll to Top