Treeview check change

  • Thread starter Thread starter mabond
  • Start date Start date
M

mabond

I'm using a treeview control.
One parent node, several child nodes, several grandchild nodes.
Each of the nodes has a checkbox.

My question, when the user changes the check value of one of the grandchild
nodes to true how do I automatically change that node's parent to check-true
and so on up the tree.....and in reverse if the parent is changed to
check-false how do I cascade that down to each of the child nodes and their
child nodes.

Any help appreciated

Michael Bond
 
I'm using a treeview control.
One parent node, several child nodes, several grandchild nodes.
Each of the nodes has a checkbox.

My question, when the user changes the check value of one of the grandchild
nodes to true how do I automatically change that node's parent to check-true
and so on up the tree.....and in reverse if the parent is changed to
check-false how do I cascade that down to each of the child nodes and their
child nodes.

Any help appreciated

Michael Bond
 
Untested code. To modify the parents, use the Parent property.

Dim parent as TreeNode = node.Parent

Do While parent IsNot Nothing Then
parent.Checked = False
parent = parent.Parent
Loop

To go the other way, you need to recurse through all of the children:

CheckChildren(node)

Private Sub CheckChildren(node As TreeNode)
Dim child as TreeNode = node.FirstNode

Do While child IsNot Nothing Then
child.Checked = True
CheckChildren(child)
child = child.NextNode
Loop

End Sub

While that does what you asked, I'm not sure it is what you really
want. If you check a node, all of the children get set. But if you
uncheck one of those children, only the child's parents, grandparents,
etc. will be unchecked, not the child's siblings.
 
Back
Top