Skip to content

Instantly share code, notes, and snippets.

@cakesmith
Created April 22, 2015 23:17
Show Gist options
  • Save cakesmith/efa66118a2d5a2cde9ea to your computer and use it in GitHub Desktop.
Save cakesmith/efa66118a2d5a2cde9ea to your computer and use it in GitHub Desktop.
Check all children in a TreeView
// 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