469,282 Members | 1,692 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

Trying to use a Treeview with checkboxes to get child nodes to be marked checked when parent node is checked.

Hello,

I am trying use a TreeView with checkboxes. I would like to check more than
one node and allow all child nodes of selected nodes to be checked or
unchecked with the parent is checked.

Thanks in advance for any help,

Jack
Nov 21 '05 #1
2 21452
Put this code for the AfterCheck Event for the Tree in your application. It
will recursively call itself until all levels under the tree node that was
checked are also checked.

Private Sub myTree_AfterCheck(ByVal sender As Object, ByVal e As _
System.Windows.Forms.TreeViewEventArgs) Handles MyTree.AfterCheck
Dim t As TreeNode
For Each t In e.Node.Nodes
t.Checked = e.Node.Checked
Next
End Sub
--
Dennis in Houston
"Jack" wrote:
Hello,

I am trying use a TreeView with checkboxes. I would like to check more than
one node and allow all child nodes of selected nodes to be checked or
unchecked with the parent is checked.

Thanks in advance for any help,

Jack

Nov 21 '05 #2
Here is another approach:
The following code will allow you to
1. check / uncheck all the children nodes when you check/uncheck parent
node.
2. auto check the parent node if all of its child nodes are checked.
3. auto uncheck the parent node if one of its child node is uncheck.
'\\\
Private Sub CheckChildNode(ByVal currNode As TreeNode)
'set the children check status to the same as the current node
Dim checkStatus As Boolean = currNode.Checked
For Each node As TreeNode In currNode.Nodes
node.Checked = checkStatus
CheckChildNode(node)
Next
End Sub

Private Sub CheckParentNode(ByVal currNode As TreeNode)
Dim parentNode As TreeNode = currNode.Parent
If parentNode Is Nothing Then Exit Sub
parentNode.Checked = True
For Each node As TreeNode In parentNode.Nodes
If Not node.Checked Then
parentNode.Checked = False
Exit For
End If
Next
CheckParentNode(parentNode)
End Sub

Private Sub treeview_AfterCheck(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.TreeViewEventArgs) Handles treeview.AfterCheck
RemoveHandler treeview.AfterCheck, AddressOf treeview_AfterCheck
CheckChildNode(e.Node)
CheckParentNode(e.Node)
AddHandler treeview.AfterCheck, AddressOf treeview_AfterCheck
End Sub
'///

HTH,

Kai


"Dennis" <De****@discussions.microsoft.com> wrote in message
news:04**********************************@microsof t.com...
Put this code for the AfterCheck Event for the Tree in your application.
It
will recursively call itself until all levels under the tree node that was
checked are also checked.

Private Sub myTree_AfterCheck(ByVal sender As Object, ByVal e As _
System.Windows.Forms.TreeViewEventArgs) Handles MyTree.AfterCheck
Dim t As TreeNode
For Each t In e.Node.Nodes
t.Checked = e.Node.Checked
Next
End Sub
--
Dennis in Houston
"Jack" wrote:
Hello,

I am trying use a TreeView with checkboxes. I would like to check more
than
one node and allow all child nodes of selected nodes to be checked or
unchecked with the parent is checked.

Thanks in advance for any help,

Jack

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Ian Powell | last post: by
1 post views Thread by Christian Rühl | last post: by
1 post views Thread by Christian Rühl | last post: by
6 posts views Thread by xla76 | last post: by
2 posts views Thread by =?Utf-8?B?bWFib25k?= | last post: by
1 post views Thread by =?Utf-8?B?amV6MTIzNDU2?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.