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 );}}}}