We help IT Professionals succeed at work.
Get Started

C# - Winforms - TreeView - Multiple Nodes

CipherIS asked
Last Modified: 2016-05-21
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)
            trvCheckLock = true;
                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;
                        bool allChecked = true;
                        foreach (TreeNode sibling in item.Parent.Nodes)
                            if (!(allChecked = allChecked && sibling.Checked))
                        item.Parent.Checked = allChecked;
                //Update the list of the selected items to the right
                if (item.Parent != null)
                trvCheckLock = false;

Open in new window

Watch Question
Distinguished Expert 2019
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE