Q: how to check automatically a parent In a Form I have a TreeView with a checkbox tree. For all child nodes in the tree I want to create an event to check if the root node is checked, and uncheck in the correct situation. For this do I necessarily need an extension class? 
Can someone give me a step by step solution? I already tried a lot of solutions; but they don't do anything. I tried to modify my code and it doesn't work. My code is with an extension class. 
Is there simple solution available for this?
|_| Parent
  |_| Child1
    |_| Child2
|_|Parent2
   |_|Child3

This is my treeView, before the name is a check box . When Parent is check automatically check Child1 and Child2. when Parent2 is check, automatically check Child3 .
namespace Extension
{

    public static class ExtensionClass
    {
        public static List<TreeNode> Parents(this TreeNode node)
        {
            var parents = new List<TreeNode>();
            TreeNode parent=node.Parent;
            while(parent!=null);
            {
                parents.Add(parent);
                parent = parent.Parent;
            }
            return parents;
        }

        public static void CheckChildren(this TreeNode node)
        {
            if (!node.Checked)
                return;

            foreach (TreeNode child in node.Nodes)
            {
                child.Checked = true;
                child.CheckChildren();
            }
        }

        public static void CheckParentsAndChildren(this TreeNode node)
        {
            node.CheckChildren(); // Check children of current node only.
            TreeNode parent = node.Parent;
            while (parent != null) ;
            {
                parent.Checked = true;
                parent = parent.Parent;
            }
        }
    }

}
namespace WindowsFormsApplication2
{

    static class Program
    {
}

and in Form2 I have this: 
using Extension;

namespace WindowsFormsApplication2
{
    public partial class Form2 : Form
    {
 private bool _checking;
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
          //  e.Node.CheckParentsAndChildren();
            if (!_checking && e.Node.Checked)
            {
                _checking = true;
                try
                {
                    e.Node.CheckParentsAndChildren();
                }
                finally
                {
                    _checking = false;
                }
            }

        }
}


A: If I understood you correctly this should already solve your issue:
private void OnTreeViewAfterCheck(object sender, TreeViewEventArgs e)
{
    var treeView = (TreeView)sender;

    treeView.AfterCheck -= OnTreeViewAfterCheck;
    SetChildCheckedState(e.Node);
    treeView.AfterCheck += OnTreeViewAfterCheck;
}

private void SetChildCheckedState(TreeNode treeNode)
{
    foreach (TreeNode childNode in treeNode.Nodes)
    {
        childNode.Checked = treeNode.Checked;

        // Call recursively if you like
        SetChildCheckedState(childNode);
    }
}

If you like, you can put that code into your own method or extension method, but I think it is a quite easy code. If you need a more complex logic please describe it here.
