Click here to Skip to main content
15,799,257 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:

I have a treeview within directories of all drives including checkboxes (Netframework 2.0) When I check one node the following procedure below checks all the childs of the checked node. Therefore I have called the procedure inside the Treeview1_AfterCheck event procedure. But my aim is also to also check the node which is tagged with a driver name such as (C:\, D:\ maybe E:\ or something else). When I do that I am getting stackoverflow error because of recursive calls. How could you perform both actions at once to check the main node (driver name) and the nodes under the other checked one? Somehow I have to get rid of CheckRootNode procedure. Any sample code will be appreciated.


<br /><br />    '<br />    'This procedure checks the root node of any child under it<br />    '<br />    Private Sub CheckRootNode(ByVal GetNode As TreeNode)<br /><br />        Do While Not GetNode.Parent Is Nothing<br /><br />            Root = GetNode.Parent<br />            CheckRootNode(Root)<br />        Loop<br /><br />        TreeView1.Nodes(Root.Index).Checked = True<br />    End Sub<br /><br />    '<br />    'This procedure checks all the child nodes of the parent<br />    '<br />    Private Sub CheckChildNodes(ByVal GetParentNode As TreeNode, ByVal CheckState As Boolean)<br /><br />        For Each xnode As TreeNode In GetParentNode.Nodes<br />            xnode.Checked = CheckState<br />        Next<br /><br />    End Sub<br />

<br /><br />Private Sub TreeView1_AfterCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterCheck<br /><br />        Call CheckChildNodes(e.Node, e.Node.Checked)<br />        Call CheckRootNode(e.Node)<br />End Sub<br />


1 solution

Your issue is that the event fired when the check state changes, is setting the check state. Add a boolean, and use it to stop the event fired in CheckChildNodes from causing CheckChildNodes to be called again from the event handler.

Share this answer

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