|
-
Jul 6th, 2011, 10:55 PM
#1
Thread Starter
Fanatic Member
Treeview - Check all Child Nodes on checking parent node
I was wondering how I would go about placing a check in the checkboxes for each child node of a parent node in a TreeView control, if I was to check the parent node.
For example:
If I was to check "Parent 1" how would I be able to make that event place a checkbox beside all of the child nodes of "Parent 1"? (Child1, 2, and 3.)
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Jul 6th, 2011, 11:05 PM
#2
Re: Treeview - Check all Child Nodes on checking parent node
Handle the AfterCheck event of the TreeView. In the event handler, get the node, test whether it's checked or not and, if it is, loop through its Nodes collection and check each item. That will recurse of its own accord.
-
Jul 6th, 2011, 11:10 PM
#3
Re: Treeview - Check all Child Nodes on checking parent node
Actually, I take back what I said about that recursing of its own accord. I just read the documentation and the event is only raised if the node is checked through the UI.
The documentation for the AfterCheck event actually has a code example that does exactly what you want to do. As such, you should have been able to answer this question for yourself. You knew that you wanted to do something with the TreeView control so the first thing you should have done is read the documentation for the TreeView control. In doing so, you would have seen that there was an AfterCheck event that was raised after a node is checked so you would have followed the link to the documentation for that event. You then would have seen that code example. Always read the documentation first and you will find many, if not most, of your questions answered.
-
Jul 6th, 2011, 11:30 PM
#4
Thread Starter
Fanatic Member
Re: Treeview - Check all Child Nodes on checking parent node
I already know about the AfterCheck event, I have handles on it right now actually in my code. I'm just not sure how I would loop through the sub/child nodes of a specific parent node, and check them all, or uncheck them all based on the checkstate of the parent.
Where is this documentation btw?
I have lots of different "depths" or "levels" inside my TreeView control, but i've never used a lot of the control in the past. How would you define specific "levels" based on the treeview expansion areas?
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Jul 6th, 2011, 11:39 PM
#5
Re: Treeview - Check all Child Nodes on checking parent node
The documentation is the MSDN Library, which you should have installed with VS or VB Express and then access via the Help menu, where every Windows application makes its documentation available. If you haven't installed it then I strongly suggest that you do. If you don't want to for some reason, you can still access it online.
A tree is an inherently recursive structure and the code example I mentioned uses recursion to go from one parent to all its children and all their children and so on.
There is no specific differentiation amongst nodes in a TreeView. If you want to differentiate then you can write the code to do so based on the Index and/or Level properties of the nodes.
-
Jul 7th, 2011, 12:03 AM
#6
Thread Starter
Fanatic Member
Re: Treeview - Check all Child Nodes on checking parent node
Hmm, thanks for all the info, I personally don't use the MSDN Library that often, and i've never accessed it through the help menu in Visual Basic. Only online a couple of times, I've usually figured out most of what I know by trial and error, and looking at how others go about using a code to a certain advantage.
As a result, I think I understand the code a lot more than someone who just visits MSDN to copy and paste what they see. But I think I need to look at how recursion works with the TreeView control. It's still fresh for me, as I'm twice as familiar with other controls like listboxes, listviews, checklistboxes, etc.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Jul 7th, 2011, 12:07 AM
#7
Thread Starter
Fanatic Member
Re: Treeview - Check all Child Nodes on checking parent node
Ohh, one more thing I was wondering. Here is my application. I want to be able to group nodes together based on their keys. How would I combine the child nodes into a single parent node so that no duplicates are visible?
MD5 hashes of the file are present for a node, but I have duplicates of those MD5 value keys, and I want to combine all of the child nodes with the same MD5 key into only one node.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Jul 7th, 2011, 12:10 AM
#8
Re: Treeview - Check all Child Nodes on checking parent node
This is some freehand typed code, so might contain errors. But it should give you an idea how to go about it.
Code:
Sub CheckChildNodes(byval parent as TreeNode, checked as Boolean)
For each child as TreeNode in parent.Nodes
child.Checked=checked
If child.Nodes.Count > 0 Then CheckChildNodes(child, checked)
Next
End Sub
-
Jul 7th, 2011, 12:33 AM
#9
Re: Treeview - Check all Child Nodes on checking parent node
 Originally Posted by AceInfinity
As a result, I think I understand the code a lot more than someone who just visits MSDN to copy and paste what they see.
I certainly wasn't suggesting that you, or anyone, should just go to MSDN to copy and paste. You must not have seen to many of my posts if you think that. I'm all about reading the documentation to get the hows and whys of each type and member so that you get a proper understanding. Trial and error has its place but it's a big waste of time if there's a clear explanation available in the documentation, as there is on this occasion.
EVERY time I have a question when I already know what type or member I need to use, I visit the MSDN Library first. I generally do even if I don't know what type or member I need, unless experience tells me that I'm likely to find the answer quicker elsewhere. That's what I've done the whole of my professional career and it's one of the primary reasons that I have been able to be a Microsoft MVP for the last five years. There's no reason that everyone else shouldn't do the same thing.
-
Jul 7th, 2011, 12:49 AM
#10
Thread Starter
Fanatic Member
Re: Treeview - Check all Child Nodes on checking parent node
I can't seem to get the code to work
I have:
Code:
Sub CheckChildNodes(ByVal Parent As TreeNode, ByVal Checked As Boolean)
For Each child As TreeNode In Parent.Nodes
child.Checked = Checked
If child.Nodes.Count > 0 Then
CheckChildNodes(child, Checked)
End If
Next
End Sub
However i'm calling it with
Code:
CheckChildNodes(ParentNodeHash, True)
And no change in the checks seems to happen. It's in the code with the handle on TreeView1.AfterCheck
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Jul 7th, 2011, 01:02 AM
#11
Re: Treeview - Check all Child Nodes on checking parent node
This is the code from the MSDN documentation:
vb.net Code:
' Updates all child tree nodes recursively. Private Sub CheckAllChildNodes(treeNode As TreeNode, nodeChecked As Boolean) Dim node As TreeNode For Each node In treeNode.Nodes node.Checked = nodeChecked If node.Nodes.Count > 0 Then ' If the current node has child nodes, call the CheckAllChildsNodes method recursively. Me.CheckAllChildNodes(node, nodeChecked) End If Next node End Sub ' NOTE This code can be added to the BeforeCheck event handler instead of the AfterCheck event. ' After a tree node's Checked property is changed, all its child nodes are updated to the same value. Private Sub node_AfterCheck(sender As Object, e As TreeViewEventArgs) Handles treeView1.AfterCheck ' The code only executes if the user caused the checked state to change. If e.Action <> TreeViewAction.Unknown Then If e.Node.Nodes.Count > 0 Then ' Calls the CheckAllChildNodes method, passing in the current ' Checked value of the TreeNode whose checked state changed. Me.CheckAllChildNodes(e.Node, e.Node.Checked) End If End If End Sub
It works perfectly as is. If you want to just check children and not uncheck them when their parent is unchecked, it's a small change:
Code:
' Updates all child tree nodes recursively.
Private Sub CheckAllChildNodes(treeNode As TreeNode, nodeChecked As Boolean)
Dim node As TreeNode
For Each node In treeNode.Nodes
node.Checked = nodeChecked
If node.Nodes.Count > 0 Then
' If the current node has child nodes, call the CheckAllChildsNodes method recursively.
Me.CheckAllChildNodes(node, nodeChecked)
End If
Next node
End Sub
' NOTE This code can be added to the BeforeCheck event handler instead of the AfterCheck event.
' After a tree node's Checked property is changed, all its child nodes are updated to the same value.
Private Sub node_AfterCheck(sender As Object, e As TreeViewEventArgs) Handles treeView1.AfterCheck
' The code only executes if the user caused the checked state to change.
If e.Action <> TreeViewAction.Unknown AndAlso e.Node.Checked Then
If e.Node.Nodes.Count > 0 Then
' Calls the CheckAllChildNodes method, passing in the current
' Checked value of the TreeNode whose checked state changed.
Me.CheckAllChildNodes(e.Node, e.Node.Checked)
End If
End If
End Sub
-
Jul 7th, 2011, 01:27 AM
#12
Thread Starter
Fanatic Member
Re: Treeview - Check all Child Nodes on checking parent node
That seemed to work, thanks, I did have to make a few modifications though, but I finally got it to work.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Jul 7th, 2011, 04:44 AM
#13
Re: Treeview - Check all Child Nodes on checking parent node
Hello AceInfinity,
Your new question was split into it's own thread here:
http://www.vbforums.com/showthread.php?t=654522
Gary
-
Jul 7th, 2011, 10:55 PM
#14
Thread Starter
Fanatic Member
Re: Treeview - Check all Child Nodes on checking parent node
Alright, sorry about that, thankyou for taking the time to edit my thread
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Jul 8th, 2011, 12:59 AM
#15
Re: Treeview - Check all Child Nodes on checking parent node
 Originally Posted by AceInfinity
Alright, sorry about that, thankyou for taking the time to edit my thread 
Not a problem, we are here to help 
Gary
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|