Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How can I select/unselect treeview checkboxes

Posted on 2006-11-25
5
Medium Priority
?
586 Views
Last Modified: 2008-03-04
Hi,

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.
0
Comment
Question by:i950
  • 3
  • 2
5 Comments
 
LVL 18

Accepted Solution

by:
Ravi Singh earned 2000 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);
}
0
 
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
0
 

Author Comment

by:i950
ID: 18013268
Hi,

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.
0
 

Author Comment

by:i950
ID: 18013552
Here's a picture of what I'm talking about:

http://www.codeproject.com/cs/miscctrl/treeviewadv/treeviewadv3.png
0
 
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:

http://www.codeproject.com/cs/miscctrl/treeviewadv.asp
0

Featured Post

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

886 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