14.9. treeView ControlThe treeView control displays nodes hierarchically in a tree. Traditionally, nodes are objects that contain values and can refer to other nodes. A parent node contains child nodes, and the child nodes can be parents to other nodes. Two child nodes that have the same parent node are considered sibling nodes. A tree is a collection of nodes, usually organized in a hierarchical manner. The first parent node of a tree is the root node (a treeView can have multiple roots). For example, the file system of a computer can be represented as a tree. The top-level directory (perhaps C:\) would be the root, each subfolder of C:\ would be a child node and each child folder could have its own children. treeView controls are useful for displaying hierarchal information, such as the file structure that we just mentioned. We cover nodes and trees in greater detail in Chapter 24, Data Structures. Figure 14.24 displays a sample treeView control on a Form. Figure 14.24. treeView displaying a sample tree.A parent node can be expanded or collapsed by clicking the plus box or minus box to its left. Nodes without children do not have these boxes. The nodes in a TReeView are instances of class treeNode. Each treeNode has a Nodes collection (type TReeNodeCollection), which contains a list of other TReeNodesknown as its children. The Parent property returns a reference to the parent node (or Nothing if the node is a root node). Figure 14.25 and Fig. 14.26 list the common properties of treeViews and treeNodes, common treeNode methods and a common treeView event.
To add nodes to the treeView visually, click the ellipsis next to the Nodes property in the Properties window. This opens the TreeNode Editor (Fig. 14.27), which displays an empty tree representing the treeView. There are Buttons to create a root, and to add or delete a node. To the right are the properties of current node. Here you can rename the node. Figure 14.27. TreeNode Editor.To add nodes programmatically, first create a root node. Create a new treeNode object and pass it a String to display. Then call method Add to add this new treeNode to the treeView's Nodes collection. Thus, to add a root node to treeView myTreeView, write myTreeView.Nodes.Add(rootLabel) where myTreeView is the treeView to which we are adding nodes, and rootLabel is the text to display in myTreeView. To add children to a root node, add new treeNodes to its Nodes collection. We select the appropriate root node from the TReeView by writing myTreeView.Nodes(myIndex) where myIndex is the root node's index in myTreeView's Nodes collection. We add nodes to child nodes through the same process by which we added root nodes to myTreeView. To add a child to the root node at index myIndex, write myTreeView.Nodes(myIndex).Nodes.Add(childNodeText) Class FrmTreeViewDirectoryStructure (Fig. 14.28) uses a TReeView to display the contents of a directory chosen by the user. A TextBox and a Button are used to specify the directory. First, enter the full path of the directory you want to display. Then click the Button to set the specified directory as the root node in the TReeView. Each subdirectory of this directory becomes a child node. This layout is similar to the one used in Windows Explorer. Folders can be expanded or collapsed by clicking the plus or minus boxes that appear to their left. Figure 14.28. treeView used to display directories.
When the user clicks the btnEnter, all the nodes in treDirectory are cleared (line 10). Then the path entered in txtInput is used to create the root node. Line 16 adds the directory to treDirectory as the root node, and line 19 calls method PopulateTreeView (lines 2861), which receives as arguments a directory (as a String) and a parent node (as a treeNode). Method PopulateTreeView then creates child nodes corresponding to the subdirectories of the directory it receives as an argument. PopulateTreeView (lines 2861) obtains a list of subdirectories, using method Getdirectories of class Directory (namespace System.IO ) in lines 3132. Method Getdirectories receives the current directory (as a String ) and returns an array of Strings representing its subdirectories. If a directory is not accessible for security reasons, an UnauthorizedAccessException is thrown. Lines 5859 catch this exception and add a node containing "Access Denied." If there are accessible subdirectories, lines 4546 use the Substring method to increase readability by shortening the full path name to just the directory name. Next, each String in the directoryArray is used to create a new child node (line 49). We use method Add (line 52) to add each child node to the parent. PopulateTreeView calls itself recursively on each subdirectory (line 55), which eventually populates the treeView with the entire directory structure. Note that our recursive algorithm may cause a delay when the program loads directories with many subdirectories. However, once the folder names are added to the appropriate Nodes collection, they can be expanded and collapsed without delay. In the next section, we present an alternative algorithm to solve this problem. |