Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
very tired with finding & implementing solution like,
If I checked
PARENT -Check all the Childs &
if Unchecked any single CHILD, child of it & PARENT of it should be unchecked see,
[√] Parent
|-[√]Child 1
|-[√]Child 1.1
|-[√]Child 1.2
|-[√]Child2
|-[√]Child 2.1
|-[√]Child 2.2


And Then

[ ] Parent
|-[√]Child 1
|-[√]Child 1.1
|-[√]Child 1.2
|-[ ]Child2
|-[ ]Child 2.1
|-[ ]Child 2.2

On unchecking on Child2
PARENT should be unchecked.
Posted
Comments
Richard Deeming 20-Jan-16 8:22am    
You need to tell us which of the hundreds of "treeview" classes you're using.
Member 10950038 20-Jan-16 8:33am    
Hello,
Thanks for being there, I need help for TreeView control in C# 3.5 .
Richard Deeming 20-Jan-16 8:36am    
Yes, but WHICH of the hundreds of classes called "TreeView" are you using?

There's one built-in for ASP.NET WebForms; there's one built-in for Windows Forms; there's one built-in for WPF; and there are hundreds of third-party controls.

The code you use will vary depending on which control you're using.

If you don't tell us which control you're using, we can't tell you how to solve the problem.
Member 10950038 20-Jan-16 9:03am    
Oh sorry,
I need code for winform application in C# 3.5 .net framework.
Sergey Alexandrovich Kryukov 20-Jan-16 12:08pm    
Do you mean System.Windows.Forms.TreeView?
If so, this is exactly what you had to specify, nothing else. It's the full type name.
"Winform application", strictly speaking, still does not specify what exactly you use.
—SA

1 solution

Please see our comments to the question. Most likely, you mean System.Windows.Forms.TreeView.

If so, you can capture the event of changing checked state of your parent node by handling one of both of these events:
TreeView.AfterCheck Event (System.Windows.Forms)[^],
TreeView.BeforeCheck Event (System.Windows.Forms)[^].

The check of other nodes is performed using the property System.Windows.Forms.TreeNode.Checked:
TreeNode.Checked Property (System.Windows.Forms)[^].

You will need the node which is about to change or changed its check state. The reference to this node will be passed to your event handler via the event arguments parameter:
TreeViewEventArgs Class (System.Windows.Forms)[^],
TreeViewEventArgs.Node Property (System.Windows.Forms)[^].

You should be extra careful here, to avoid infinite recursion. When you programmatically change the checked state in your event handler, it will eventually call the same event handler. So, you need different action on the change of this state as an immediate result of the user click and as a result of your programmatic change.

Again, look at this page I already referenced above: TreeView.AfterCheck Event (System.Windows.Forms)[^].

See the "Remark" section which warns you about this situation. See also the code sample; pay attention for the comment "The code only executes if the user caused the checked state to change". In your code, you should also take care of that.

—SA
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900