C# - Winforms - TreeView - Multiple Nodes

Apparently my question has been neglected so I am opening up a new question as I need a solution.

I have the Below code.  It works fine for a TreeView with two nodes.  I have three nodes that I am working on and need to  expand it.  So, it my data looks something like this.

-  Level 1
   -  Sub Level  2
      -  Sub Sub Level 3

When I  click on  Sub Sub Level 3 I  want all nodes  above check if all nodes above "Sub Sub Level 3 are checked" and unchecked  also (the reverse).  Right now it will check "Sub Sub Level 3" and "Sub Level 2" but not  "level 1".  I also want all Nodes below Level 1 checked if Level 1 is checked.

        private void myTreeView_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (trvCheckLock)
            {
                return;
            }
            trvCheckLock = true;
            trvMessageType.BeginUpdate();
            try
            {
                TreeNode item = e.Node;
                //If the user checks/unchecks the node that has children,
                if (item.Nodes.Count > 0)
                {
                    //All children are unchecked as well
                    foreach (TreeNode child in item.Nodes)
                    {
                        child.Checked = item.Checked;
                        UpdateObjectModel(item.Text, child.Text, child.Checked);
                    }
                }
                //Otherwise (if the item has a parent)
                if (item.Parent != null)
                {
                    //The parent node should be checked only when all its children 
                    //are checked too
                    if (!item.Checked)
                    {
                        item.Parent.Checked = false;
                    }
                    else
                    {
                        bool allChecked = true;
                        foreach (TreeNode sibling in item.Parent.Nodes)
                        {
                            if (!(allChecked = allChecked && sibling.Checked))
                            {
                                break;
                            }
                        }
                        item.Parent.Checked = allChecked;
                    }
                }
                //Update the list of the selected items to the right
                if (item.Parent != null)
                    UpdateSelected(item);
            }
            finally
            {
                trvMessageType.EndUpdate();
                trvCheckLock = false;
            }
        }

Open in new window

LVL 2
CipherISAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
Something like this:

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

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private Boolean trvCheckLock;

        public Form1()
        {
            InitializeComponent();

            trvMessageType.ExpandAll();
            trvMessageType.AfterCheck += myTreeView_AfterCheck;
        }

        private void myTreeView_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (trvCheckLock)
            {
                return;
            }
            trvCheckLock = true;
            trvMessageType.BeginUpdate();
            try
            {
                TreeNode item = e.Node;

                SetChildrenChecked(item, e.Node.Checked);
                SetParentChecked(item, e.Node.Checked);
            }
            finally
            {
                trvMessageType.EndUpdate();
                trvCheckLock = false;
            }
        }

        protected void SetChildrenChecked(TreeNode pNode, bool pChecked)
        {
            if (pNode.Nodes.Count > 0)
            {
                foreach (TreeNode node in pNode.Nodes)
                {
                    node.Checked = pChecked;
                    if (node.Nodes.Count>0)
                        SetChildrenChecked(node, pChecked);
                }
            }
        }

        protected void SetParentChecked(TreeNode pNode, bool pChecked)
        {
            if (pNode.Parent != null)
            {

                pNode.Parent.Checked = pChecked;
                SetParentChecked(pNode.Parent, pChecked);
            }
        }

    }
}

Open in new window

CipherISAuthor Commented:
Overflow exception.  Trying to figure it out.
it_saigeDeveloperCommented:
Refer to the answer I provided in your other question.

-saige-

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Éric MoreauSenior .Net ConsultantCommented:
have you tried just that code before trying to integrate your other call to UpdateObjectModel/...?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.