0
votes

I'm using .net c# in VS 2012 and trying to write a function that will auto-check all child nodes of a checked parent treeview node. I'm new at c# programming so I found the following code but it has two problems:

  1. The line if (HasChildNodes(node)) visual studio reports HasChildNodes is not known.
  2. I want to start with the selected node I click check on and I think the code traverses the whole tree?

Thanks for any help.

        treeView.BeginUpdate();
        //Loop through all the nodes of tree
        foreach (TreeNode node in treeView.Nodes)
        {
            //If node has child nodes
            if (HasChildNodes(node))
            {
                if (node.Checked == true)
                {
                    //Check all the child nodes.
                    foreach (TreeNode childNode in node.Nodes)
                    {
                        childNode.Checked = true;
                    }
                }
            }
        }
        treeView.EndUpdate();
7

7 Answers

4
votes

Below is the working code for tree view sample with checking and unchecking of child and parent node (Add a treeview and assign name as tvwDynamic)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Treeview { public partial class Form1 : Form { public Form1() { InitializeComponent(); }

    private void Form1_Load(object sender, EventArgs e)
    {
        TreeNode MainNode = new TreeNode("Java");
        tvwDynamic.Nodes.Add(MainNode);
        MainNode = new TreeNode("PHP");
        tvwDynamic.Nodes.Add(MainNode);           
        TreeNode node2 = new TreeNode("C#");
        TreeNode node3 = new TreeNode("VB.NET");
        TreeNode[] childNodes = new TreeNode[] {node2,node3};
        MainNode = new TreeNode("ASP.NET", childNodes);
        tvwDynamic.Nodes.Add(MainNode);
         TreeNode node4 = new TreeNode("Winforms");
        TreeNode node5 = new TreeNode("Webforms");
          TreeNode[] SubchildNodes = new TreeNode[] {node4,node5};
        MainNode =  new TreeNode("Test",SubchildNodes);
        tvwDynamic.Nodes[2].Nodes[1].Nodes.Add(MainNode);
        tvwDynamic.CheckBoxes = true;

    }

    private void tvwDynamic_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);

            }

        }

        SelectParents(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);

            }

        }

    }


    private void SelectParents(TreeNode node, Boolean isChecked)
    {
        var parent = node.Parent;

        if (parent == null)
            return;

        if (!isChecked && HasCheckedNode(parent))
            return;

        parent.Checked = isChecked;
        SelectParents(parent, isChecked);
    }

    private bool HasCheckedNode(TreeNode node)
    {
        return node.Nodes.Cast<TreeNode>().Any(n => n.Checked);
    }
} }
2
votes
 treeview1.BeginUpdate();
 foreach (TreeNode tn in e.Node.Nodes)
     tn.Checked = e.Node.Checked;
 treeview1.EndUpdate();

This is working for me. you have to add this code inside AfterCheck EventHandler.

1
votes

Here's a simple Control you can use in Visual Studio's Designer:

public class BetterTreeView : TreeView
{
    /// <summary>
    /// Whether to apply Checked property changes to child nodes.
    /// </summary
    public bool TickChildNodes { get; set; }

    protected override void OnAfterCheck(TreeViewEventArgs e)
    {
        base.OnAfterCheck(e);

        if (TickChildNodes)
        {
            foreach (TreeNode node in e.Node.Nodes)
            {
                node.Checked = e.Node.Checked; // Triggers OnAfterCheck (recursive)
            }
        }
    }
}
0
votes

There is not any HasChildNodes method related to TreeView, you were mixed up with XML stuff here.

public void CheckNodes(TreeNode startNode){
   startNode.Checked = true;
   foreach(TreeNode node in startNode.Nodes)
      CheckNodes(node);       
}        
//then you can call the method above with the TreeNode you check
CheckNodes(yourNode);
0
votes

to check if you have child nodes it suffice to check

node.FirstNode != null

as for your second question it looks like your code check the hole tree and not just from the given node, then again i need to see the entire method signature and where do you keep the pressed node. if you have it in a member like myMember then change the treeView in that method with myMember:

    treeView.BeginUpdate();
    //Loop through all the nodes of tree
    foreach (TreeNode node in myMember.Nodes)
    {
        //If node has child nodes
        if (node.FirstNode != null)
        {
            if (node.Checked == true)
            {
                //Check all the child nodes.
                foreach (TreeNode childNode in node.Nodes)
                {
                    childNode.Checked = true;
                }
            }
        }
    }
    treeView.EndUpdate();
0
votes

Corrected foreach loop:

foreach (TreeNode node in treeView1.Nodes)
        {
            //If node has child nodes
            if (node.Checked == true)   //it is better to first check if it is "checked" then proceed to count child nodes
            {
                if (node.GetNodeCount(false) > 0)   //check if node has any child nodes
                {
                    //Check all the child nodes.
                    foreach (TreeNode childNode in node.Nodes)
                    {
                        childNode.Checked = true;
                    }
                }
            }
        }
0
votes

Check the parant node is check..

Then go though all child nodes..

if(e.Node.Checked==true)
            {
                foreach (TreeNode tn in e.Node.ChildNodes)
                {
                    tn.Checked = true;
                }
            }