Treeview recursive check

psyfect

New member
Joined
Sep 25, 2006
Messages
3
Programming Experience
Beginner
Why would this code not work?

Private Sub TreeView1_BeforeCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeCheck
Dim nd As TreeNode
For Each nd In TreeView1.SelectedNode.Nodes
Debug.Write(nd.FullPath)
Next
nd = Nothing
End Sub
End Class

Now in the debug window if I do:
Debug.Write(nd.FullPath)
Debug.Write(nd.Text)
Debug.Write(nd.Checked)

I get the correct information, however, if I change "Debug.Write(nd.FullPath)" to nd.checked = true I get the following error:
"An unhandled exception of type 'System.StackOverflowException' occurred in system.windows.forms.dll"

I've got a recursive search function that works great, but it iterates through all nodes. I'm trying to do it this way because this method iterates through only the direct childern of the parent and not every child including there children and childrens children, etc, etc.
 
hi,


i think its because you are using a "BeforeCheck" event, the node doesnt recognise that it is selected. why did you select a "beforecheck" event? i think the even you want is either a click event or when the node is selected.

hope it helps mate! :)

regards
adam
 
A checked node is one that is ticked

A selected node is one that is blue highlighted

You can check a node without selecting it.

You can select a node without checking it

Decide whether you want to print the children of a node that is checked/ticked or whether you want to print the children of a node that is selected

Use the correct event (AfterCheck or AfterSelect)

The event receives an argument of TreeViewEventArgs

TreeViewEventArgs has a .Node property that allows you to know what node is being interacted with

Use that instead of guessing that MyTreeView1.SelectedWhatever will give you what you want
 
Back
Top