Generally, we need more functions by expanding a parent node and expanding all child nodes (I .e., linkage) at the same time ), but sometimes we also need to expand all the parent nodes on a node until the current node, for example, when deleting a node, we do not want to delete it and all nodes are collapsed, this function is required at this time. In this case, it may be hard to understand or express myself in poor language. Let's look at a picture:
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + encrypt/decrypt + decrypt/b3ateOhsDExMTExobGjrMm + s/3WrrrzysfX1Lav1bm/decrypt + cjxwpjxpbwcgc3jjjpq = "http://www.2cto.com/uploadfile/Collfiles/20140518/20140518092945218.png" alt = "\">
Note that the two graphs are different.
How to implement this function?Recursion!
The key to recursion is to find the parent node of the node to be deleted (for example, C), find the parent node C, and then use recursion to find the parent node B of C, find the parent node B and continue to find the parent node A of B ........ you also need to add a judgment to determine the parent node. parent. part of the Code for Parent:
String parentName = ""; private void Delete selected node ToolStripMenuItem_Click (object sender, EventArgs e) {try {TreeNode node = treeView1.SelectedNode. parent; if (ChuLi. deleteNode (NodeId) // This is a method defined by myself. You can ignore {if (node! = Null) // if the child node is not the root node, expand {parentName = node. text; // expand the parent node ExpandNodes (treeView1.Nodes); // call ---------------------------------------------------} else {MessageBox. show ("deletion failed! ") ;}} Catch (Exception ex) {MessageBox. show (ex. message) ;}} // traverse all nodes to find the specified private void ExpandNodes (TreeNodeCollection tnodes) {foreach (TreeNode node in tnodes) {if (node. text = parentname) // compare, so you need to know the parent node name of the selected node: node. parent. text {ExpandParentNodes (node. parent. nodes); // call the next Recursive Method} ExpandNodes (node. nodes) ;}} // private void ExpandParentNodes (TreeNodeCollection tnodes) {TreeNode parentNode; foreach (TreeNo De node in tnodes) {parentNode = node; if (parentNode. Parent! = Null) {parentNode. Parent. Expand (); if (parentNode. Parent. Parent! = Null) // determines whether the parent node of the parent node is null. If the root node is reached, it is null {ExpandParentNodes (parentNode. parent. parent. nodes);} elsebreak; // you have reached the root node and exited }}}
The above code can be used to expand all the parent nodes of the child node.
The rush of time, limited technical capabilities, and the shortcomings are worth your attention!
Knowledge is used for sharing, but please indicate the source for reprinting! Thank you for your cooperation!