C # perform the following operations on the Treeview component:
Although the Treeview component is difficult to operate, in the final analysis, it can be summarized as three basic operations: adding a child node, adding a sibling node, and deleting a node. Having mastered these three common operations is necessary for the flexible use of the Treeview component in programming. Next we will introduce them separately.
(1). Add a subnode:
A subnode is the next-level node of the selected node. The specific process of adding a child node is as follows: first, locate the position of the child node to be added in the Treeview component, and then create a Node object, then, add the Node object using the add () method in the treeveiw class. The following describes how to add a subnode to the treeview1 component.Code:
// First, determine whether to select the position in the component. If (treeview1.selectednode = NULL) { MessageBox. Show ("select a node", "message", messageboxbuttons. OK, messageboxicon. information ); } Else { // Create a Node object and initialize it Treenode TMP; TMP = new treenode ("node name "); // Add a subnode to the Treeview component Treeview1.selectednode. nodes. Add (TMP ); Treeview1.selectednode = TMP; Treeview1.expandall (); } |
(2) Add a sibling node:
A sibling node is a level node on the selected node. The method for adding a sibling node is basically the same as that for adding a child node, but there is a slight difference in the final implementation method. To add a sibling node, you must first determine the location of the sibling node to be added, define a Node object, and call the method to add a sibling node in the Treeview class, add this Node object. The biggest difference between joining a sibling node and joining a subnode is this last step. Hope you can pay attention to it. The following code adds a sibling node to the Treeview component:
// First determine whether to select the node location in the component If (treeview1.selectednode = NULL) { MessageBox. Show ("select a node", "message", messageboxbuttons. OK, messageboxicon. information ); } Else { // Create a Node object and initialize it Treenode TMP; TMP = new treenode (textbox1.text ); // Add a sibling node to the Treeview component Treeview1.selectednode. Parent. nodes. Add (TMP ); Treeview1.expandall (); } |
(3). delete a node:
Deleting a node is to delete the selected node in the Treeview component. Deleting a node can be a subnode or a sibling node, but regardless of the nature of the node, you must ensure that the node to be deleted does not have a lower-level node. Otherwise, you must delete all the lower-level nodes of the node before deleting the node. Deleting a node is slightly simpler than the preceding two operations. The specific method is to first determine whether the node to be deleted exists in the next-level node. If it does not exist, call remove () in the Treeview class () to delete the node. The code for deleting a node in the Treeview component is as follows:
// Determine whether the selected node has a lower-level node If (treeview1.selectednode. nodes. Count = 0) // Delete a node Treeview1.selectednode. Remove (); Else MessageBox. Show ("Delete the subnode in this node first! "," Message ", messageboxbuttons. OK, messageboxicon. information ); |
(4) Some other common operations of the Treeview component:
Some other common operations are much easier to implement than the preceding three operations. These common operations are to expand all nodes, expand the specified nodes, and collapse all nodes. Here is a detailed introduction:
<I>. Expand All nodes:
To expand all the nodes in the Treeview component, first locate the selected node pointer on the Root Node of the Treeview component, and then call the expandall method of the selected component. The following is the specific code:
// Locate the root node
Treeview1.selectednode = treeview1.nodes [0];
// Expand all nodes in the component
Treeview1.selectednode. expandall ();
<II>. Expand the next level node of the selected node:
Because it only expands the next-level node, there is no need to use the expandall () method. To expand the next-level node, you only need to call the expand () method. The following is the specific implementation code:
Treeview1.selectednode. Expand ();
<III>. collapse all nodes:
Folding all nodes and expanding all nodes is a set of interoperability, and the specific implementation ideas are also roughly the same. Folding all nodes also requires first locating the selected node pointer on the root node, then you can call the collapse () of the selected component. The specific implementation code is as follows:
// Locate the root node Treeview1.selectednode = treeview1.nodes [0]; // Collapse all nodes in the component Treeview1.selectednode. Collapse (); |
Now, the common methods and general methods for operating the Treeview component in C # have been introduced.