4

How do i check/uncheck all child items in TreeView? Probably error happened when one of items become to selected state and child items checked/unchecked buggy.

Link to VS 2010 Project+Exe

Video on YOUTUBE

private void Form1_Load(object sender, EventArgs e) { treeView1.CheckBoxes = true; treeView1.BeginUpdate(); treeView1.Nodes.Add("1111"); treeView1.Nodes[0].Nodes.Add("2222"); treeView1.Nodes[0].Nodes.Add("2222"); treeView1.Nodes[0].Nodes.Add("2222"); treeView1.Nodes[0].Nodes.Add("2222"); treeView1.Nodes[0].Nodes[1].Nodes.Add("3333"); treeView1.Nodes[0].Nodes[1].Nodes[0].Nodes.Add("4444"); treeView1.Nodes[0].Nodes[1].Nodes[0].Nodes.Add("4444"); treeView1.Nodes[0].Nodes[1].Nodes[0].Nodes.Add("4444"); treeView1.EndUpdate(); treeView1.ExpandAll(); } private void treeView1_AfterCheck(object sender, TreeViewEventArgs e) { treeView1.BeginUpdate(); foreach (TreeNode Node in e.Node.Nodes) { Node.Checked = e.Node.Checked; } treeView1.EndUpdate(); } 
17
  • What? if you need to check all child nodes then do it recursively. Commented Feb 5, 2013 at 2:21
  • You should read the Remarks section of the TreeView class MSDN page Commented Feb 5, 2013 at 2:22
  • Andrey you need to do something that calls recursively for example public void UncheckAllNodes(TreeNodeCollection nodes) { foreach (TreeNode node in nodes) { node.Checked = false; CheckChildren(node, false); } } Commented Feb 5, 2013 at 2:30
  • for the CheckChildern(node,false) do this private void CheckChildren(TreeNode rootNode, bool isChecked) { foreach (TreeNode node in rootNode.Nodes) { CheckChildren(node, isChecked); node.Checked = isChecked; } } Commented Feb 5, 2013 at 2:32
  • @DJKRAZE: Why not answer the question? Commented Feb 5, 2013 at 2:36

5 Answers 5

5

using your code that you have in the Form_Load keep that code it works and add these 2 methods in your code

private void Form1_Load(object sender, EventArgs e) { treeView1.CheckBoxes = true; treeView1.BeginUpdate(); treeView1.Nodes.Add("1111"); treeView1.Nodes[0].Nodes.Add("2222"); treeView1.Nodes[0].Nodes.Add("2222"); treeView1.Nodes[0].Nodes.Add("2222"); treeView1.Nodes[0].Nodes.Add("2222"); treeView1.Nodes[0].Nodes[1].Nodes.Add("3333"); treeView1.Nodes[0].Nodes[1].Nodes[0].Nodes.Add("4444"); treeView1.Nodes[0].Nodes[1].Nodes[0].Nodes.Add("4444"); treeView1.Nodes[0].Nodes[1].Nodes[0].Nodes.Add("4444"); treeView1.EndUpdate(); treeView1.ExpandAll(); } private void treeView1_AfterCheck(object sender, TreeViewEventArgs e) { foreach (TreeNode childNode in e.Node.Nodes) { childNode.Checked = e.Node.Checked; } } 
Sign up to request clarification or add additional context in comments.

2 Comments

DJ KRAZE That not help's. So i make video to show you what wrong. youtube.com/watch?v=kviRgkpUiMs Also i fix video link.
I provided you with a newer version I hope this corrects your problem it is working as well this is a cleaner version of code for you
1

The solution that I've figured out was the following:

  • To perform the selection I used Linq:

    if (e.Node.Nodes.Count > 0) e.Node.Nodes.Cast<TreeNode>().Where(tnc => tnc.Checked != e.Node.Checked).ForEach(tn => tn.Checked = e.Node.Checked);

  • Then to handle the event selection I extended the TreeView class: public class MyTV : TreeView { protected bool TopOfCascade; TreeNode startNode; public Action FinishedCheckEventsDelegate; public Action InitiateCascadeCheckEventsDelegate; public MyTV() : base() { TopOfCascade = false; startNode = null; } protected override void OnBeforeCheck(TreeViewCancelEventArgs e) { base.OnBeforeCheck(e); if (!TopOfCascade && !e.Cancel) { TopOfCascade = true; startNode = e.Node; if (InitiateCascadeCheckEventsDelegate != null) InitiateCascadeCheckEventsDelegate(); } } protected override void OnAfterCheck(TreeViewEventArgs e) { base.OnAfterCheck(e); if (startNode == e.Node && e.Action != TreeViewAction.Unknown) { if (FinishedCheckEventsDelegate != null) FinishedCheckEventsDelegate(); TopOfCascade = false; startNode = null; } } In the host control, you can hook the pre and post cascade events and iterate over all checked/unchecked tree nodes avoiding a lot of cross event firing. You control the cascade via some container.

I got the ForEach to IEnumerable from the following an implementation from the web:

public static void ForEach<T>(this IEnumerable<T> source, Action<T> action) { if (source == null) throw new ArgumentNullException("source"); if (action == null) throw new ArgumentNullException("action"); foreach (T item in source) { action(item); } } 

Good Luck!

Comments

0

Here is an answer (from Hans Passant):

Winforms treeview, recursively check child nodes problem

Im writing it as answer, not comment to make it easier to find.

Comments

0
 private void treeView1_AfterCheck(object sender, TreeViewEventArgs e) { if (e.Action != TreeViewAction.Unknown) { if (e.Node.Nodes.Count > 0) { /* Calls the CheckAllChildNodes method, passing in the current Checked value of the TreeNode whose checked state changed. */ this.CheckAllChildNodes(e.Node, e.Node.Checked); } } } 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. this.CheckAllChildNodes(node, nodeChecked); } } } 

Comments

0
 private void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked) { foreach (TreeNode node in treeNode.Nodes) { node.Checked = nodeChecked; if (node.Nodes.Count > 0) { this.CheckAllChildNodes(node, nodeChecked); } } } private void treeView_AfterCheck(object sender, TreeViewEventArgs e) { if (e.Action != TreeViewAction.Unknown) { if (e.Node.Nodes.Count > 0) { if (!e.Node.Checked) { this.CheckAllChildNodes(e.Node, e.Node.Checked); } } } if (e.Node.Parent != null) { TreeNode n = e.Node; while (n.Parent != null) { if (n.Checked) { n.Parent.Checked = true; } n = n.Parent; } } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.