Created
          April 22, 2015 23:17 
        
      - 
      
 - 
        
Save cakesmith/efa66118a2d5a2cde9ea to your computer and use it in GitHub Desktop.  
    Check all children in a TreeView
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | // Updates all child tree nodes recursively. | |
| private void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked) | |
| { | |
| foreach (TreeNode node in treeNode.Nodes) | |
| { | |
| node.Checked = nodeChecked; | |
| if (node.Nodes.Count > 0) | |
| { | |
| // If the current node has child nodes, call the CheckAllChildsNodes method recursively. | |
| CheckAllChildNodes(node, nodeChecked); | |
| } | |
| } | |
| } | |
| private void myTreeView1_AfterCheck(object sender, TreeViewEventArgs e) | |
| { | |
| // The code only executes if the user caused the checked state to change. | |
| if (e.Action == TreeViewAction.Unknown) return; | |
| if (e.Node.Nodes.Count > 0) | |
| { | |
| /* Calls the CheckAllChildNodes method, passing in the current | |
| Checked value of the TreeNode whose checked state changed. */ | |
| CheckAllChildNodes(e.Node, e.Node.Checked); | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment