WinForm TreeView Node Selection

Source: Internet
Author: User

 Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); }        stringTag ="True";        StringBuilder Nodestag; /// <summary>        ///Select All/// </summary>        /// <param name= "TreeNode" ></param>        /// <param name= "nodechecked" ></param>        Private voidCheckallchildnodes (TreeNode TreeNode,BOOLnodechecked) {            foreach(TreeNode nodeinchtreenode.nodes) {node. Checked=nodechecked; Node. Tag= tag;////Remember to set the Tag property for the selected item here                if(node. Nodes.count >0)                {                     This.                Checkallchildnodes (node, nodechecked); }            }        }        Private voidTraversnodes (TreeNode parent) {TreeNode node=parent; if(Node! =NULL)            {                if(node. Tag! =NULL&& node. tag.tostring () = =tag) Nodestag.appendformat ("node:{0} has checked\n", node.                Text); if(node. Firstnode! =NULL)////If node nodes have child nodes then go to traverse{traversnodes (node.                Firstnode); }                if(node. NextNode! =NULL)////If there is a sibling node behind the node, go to traverse{traversnodes (node.                NextNode); }            }        }        /// <summary>        ///Checked or Unchecked box event occurred/// </summary>        /// <param name= "Sender" ></param>        /// <param name= "E" ></param>        Private voidTreeview1_aftercheck (Objectsender, TreeViewEventArgs e) {            if(E.action! =treeviewaction.unknown) {TreeNode node=E.node; if(node. Tag = =NULL) node. Tag= tag;//Additional node Information                Elsenode. Tag=NULL;                Checkallchildnodes (E.node, e.node.checked); //Select parent Node                BOOLBol =true; if(E.node.parent! =NULL)                {                     for(inti =0; i < E.node.parent.nodes.count; i++)                    {                        if(!E.node.parent.nodes[i]. Checked) Bol=false; } e.node.parent.checked=Bol; ///Remember to set its tag if the parent node is selected or canceled                    if(BOL) {E.node.parent.tag=tag; }                    Else{E.node.parent.tag=NULL; }                }            }        }    }

WinForm TreeView Node Selection

Related Article

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.