C # In TreeView, select the parent node and select all child nodes

Source: Internet
Author: User

C # In TreeView, select the parent node and select all child nodes

TreeView is usually used with CheckBoxs to display and select some entries, especially when the levels are separated, such

When I select a parent node, I want all the child nodes under the parent node to be selected, cancel a node, and want all its parent nodes to be canceled, for example

The Code is as follows: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwcmUgY2xhc3M9 "brush: java;"> // After canceling the node selection, cancel the private void setParentNodeCheckedState (TreeNode currNode, bool state) {TreeNode parentNode = currNode. parent; parentNode. checked = state; if (currNode. parent. parent! = Null) {setParentNodeCheckedState (currNode. parent, state) ;}}// after the node is selected, all the subnodes of the selected node are private void setChildNodeCheckedState (TreeNode currNode, bool state) {TreeNodeCollection nodes = currNode. nodes; if (nodes. count> 0) {foreach (TreeNode tn in nodes) {tn. checked = state; setChildNodeCheckedState (tn, state) ;}} private void treeview?aftercheck (object sender, TreeViewEventArgs e) {if (e. action = TreeViewAction. byMouse) {textBox1.Text = e. node. text; if (e. node. checked = true) {// After selecting a node, select all the child nodes of the node setChildNodeCheckedState (e. node, true);} else if (e. node. checked = false) {// After the node is selected, deselect setChildNodeCheckedState (e. node, false); // if the Node has a parent Node, cancel the select status of the parent Node if (e. node. parent! = Null) {setParentNodeCheckedState (e. Node, false );}}}}

 

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.