C # TreeView Control parent Node PIPS nodes Tick Change Association

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.