![]() When we move a node, it takes with it the entire Nodes collection located under it. ![]() Next Code language: VB.NET ( vbnet ) Enumerating the Nodes CollectionĪs you saw in the previous example, a Node object can include an entire tree under it. Likewise, the following loop extracts the root nodes from the TVList ArrayList: Dim node As TreeNode The following loop stores all the root nodes of the TreeView1 control to the TVList ArrayList: Dim TVList As New ArrayList For a routine that will work with any tree, you must assume that the number of nodes is unknown, so the ArrayList would be a better choice. You can revise this code so that it uses an array of Node objects instead of individual variables to store all the root nodes. Listing 4.38: Moving an Entire Tree Protected Sub MoveTree Click(.) _ĭim colorNode, shapeNode, solidNode As TreeNode The code behind the Move Tree button is shown in Listing 4.38. Then we must clear the entire tree, add the new root node (the Items node), and finally add all the copied nodes under the new root. The three variables are of the TreeNode type, and they’re set to the root nodes of the original tree. There are three root nodes currently in our control, so we need three local variables. We’ll use these variables to add the current root nodes under the new (and single) root node. Before we do so, however, we must copy all the first-level nodes into local variables. It’s not a common operation, but it’s an interesting example of how to manipulate the nodes of a TreeView control at runtime.įirst, we must add the new root node. In other words, we’ll add a new node called Items that will act as the root node for existing nodes. Let’s revise the code we’ve written so far to display all the nodes under a new header. This is the Add Shapes button’s Click event handler. ![]() To add child nodes under the Shapes node, use the statements shown in Listing 4.37. I added a few statements in the TreeView Example project’s code to disable the buttons that generate similar runtime errors. If you click the Add Colors button first, you’ll get a NullReferenceException, indicating that the node can’t be inserted unless its parent node already exists. Run the project, click the first button (Add Categories), and then click the second button (Add Colors). Alternatively, you can add a statement that calls the Expand method of the cnode object, after adding the color nodes to the control: cnode.Expand() Code language: VB.NET ( vbnet ) The same TreeView control with its Colors node expanded is shown to the left. ![]() To see its child nodes, you must double-click the Colors node to expand it (or click the plus sign in front of it, if there is one). Therefore, its child nodes won’t be visible. When these statements are executed, three more nodes are added under the Colors node, but the Colors node won’t be expanded. ![]()
0 Comments
Leave a Reply. |