How can I select/unselect treeview checkboxes

Posted on 2006-11-25
Last Modified: 2008-03-04

I have a product list treeview, which contains checkbox for each item. What I'm trying to accomplish is when a treeview checkbox is selected/unselected, I want all the subitems checkboxes' selected/unselected as well.

How may I do that?

Please help. Thanks.
Question by:i950
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 18

Accepted Solution

Ravi Singh earned 500 total points
ID: 18013145
Hi, you need to handle the TreeView controls AfterCheck event and call a recursive method that selects all the child nodes. To create the event handler automatically, select the TreeView control in design mode, then click the lightning bolt icon in the Property panel... double click the AfterCheck event and the event handler should be created like below (you'll need to copy the code in). The following should work:

private void treeView1_AfterCheck(object sender, System.Windows.Forms.TreeViewEventArgs e)
      if (e.Action == TreeViewAction.ByMouse || e.Action == TreeViewAction.ByKeyboard)
            this.CheckUncheckChildNodes(e.Node, e.Node.Checked);

private void CheckUncheckChildNodes(TreeNode tn, bool checkState)
      tn.Checked = checkState;
      foreach (TreeNode tnChild in tn.Nodes)
            CheckUncheckChildNodes(tnChild, checkState);
LVL 18

Expert Comment

by:Ravi Singh
ID: 18013150
clarification: in my first sentence I meant that the code checks/unchecks the child nodes based on the parent nodes checked state

Author Comment

ID: 18013268

It's working but there's something that I thought was automatic is missing! When I have an sub item check and it's parent unchecked, I though the parent will show a green rectangle in the checkbox (in the place of the check mark, like it's the case in VS.NET installation), but that's no the case.

Is that an easy thing to do or a custom control should be created?

Thank you very much.

Author Comment

ID: 18013552
Here's a picture of what I'm talking about:
LVL 18

Expert Comment

by:Ravi Singh
ID: 18014088
Hi, I'm not sure, it doesn't seem to be a built in feature in .NET 1.1 or .NET 2's TreeView controls... I think its a custom feature, you might want to try using the TreeView control that you provided a link for:

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor ( Top Charts is a view in which you can set seve…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question