The project is used in the TreeView control, and the parent node is selected to be associated. The child node is checked and the parent node is synchronized. The parent node is checked and all child nodes are checked and recursive. Online to find a circle, a lot of code is not satisfied with the requirements. The node is checked, and the recursive sub-node is down, all implemented. Node check, recursion upward, parent node synchronization check, also implemented. However, the node removal check, should be judged by its peers all nodes, if the sibling nodes are not checked status, you should remove the parent tick state, and recursion upward. This, no suitable code was found. I wrote this part of the code myself. Just to achieve this effect, the code also has the space to optimize, the subsequent time to change.
Private voidTreeview1_aftercheck (Objectsender, TreeViewEventArgs e) { //only the events triggered by the mouse tick are executed, avoiding the code recursive execution tick, infinite loop if(E.action! =treeviewaction.bymouse)return; This. Updatenodescheckstate (E.node); } /// <summary> ///set node hierarchy status/// </summary> /// <param name= "Node" ></param> Private voidupdatenodescheckstate (TreeNode node) {//set child nodes, recursively downsetchildnode (node); //Set parent node, recursion up if(node. Checked) {//Ticksetparentnodechecked (node); } Else { //Not checked//If none of the sibling nodes are checked, the parent removes the tick. Constantly upward recursive judgmentsetparentnodeunchecked (node); } } /// <summary> ///node check, set parent node selected. And the upward recursion setting is selected/// </summary> /// <param name= "Node" ></param> Private voidsetparentnodechecked (TreeNode node) {if(node. Parent! =NULL) {node. Parent.checked=true; Setparentnodechecked (node. Parent); } } /// <summary> ///node not checked, set ancestor node status///determine sibling node state, set parent node/// </summary> /// <param name= "Node" ></param> Private voidsetparentnodeunchecked (TreeNode node) {BOOLiaallunchecked =true; if(node. Parent! =NULL) { for(inti =0; I < node. Parent.Nodes.Count; i++) { if(node. Parent.nodes[i]. Checked) {iaallunchecked=false; Break; } } } //None of the sibling nodes of the current node are checked, the parent node setting status is unchecked. Recursion Upward if(iaallunchecked && node.) Parent! =NULL) {node. Parent.checked=node. Checked; Setparentnodeunchecked (node. Parent); } } /// <summary> ///The parent node changes, its child nodes are synchronized, and all child nodes are consistent with the parent tick state. /// </summary> /// <param name= "Node" ></param> Private voidSetchildnode (TreeNode node) {foreach(TreeNode Childnodeinchnode. Nodes) {childnode.checked=node. Checked; if(node. Nodes.count >0) Setchildnode (Childnode); } }
C # TreeView Control parent Node PIPS nodes Tick Change Association