how do I exit from Recursive Loop Exit
How do I exit from the recursive loop from the code below. I would like to notify the end-user to select a checkbox in a msgBox before I exit the loop. Thanks.
Private Sub PrintRecursive(ByVal n As TreeNode)
System.Diagnostics.Debug.WriteLine(n.Text)
If (n.Checked = True) Then
MessageBox.Show(n.Checked)
Else
If (n.Checked = False) Then
MessageBox.Show("Check a bex")
End If
End If
' MessageBox.Show(n.Checked)
Dim aNode As TreeNode
For Each aNode In n.Nodes
PrintRecursive(aNode)
Next
End Sub
' Call the procedure using the top nodes of the treeview.
Private Sub CallRecursive(ByVal aTreeView As TreeView)
Dim n As TreeNode
For Each n In aTreeView.Nodes
PrintRecursive(n)
开发者_高级运维 Next
End Sub
One way would be to change PrintRecursive into a function that returns a boolean, for which true means "Stop"
Then change your recursion call to check the return value.
For Each aNode In n.Nodes
if not PrintRecursive(aNode) then
msgbox("Notify User")
return false
end if
Next
A word of warning though, the messagebox will be displayed at each level of nesting when exiting the recursion. To avoid this you could add a parameter for the nesting level to PrintRecursive so you could tell when you were at the top level.
Private Function PrintRecursive(ByVal n As TreeNode, optional byval NestLevel as Integer=0) as Boolean
...
For Each aNode In n.Nodes
if not PrintRecursive(aNode,NestLevel+1) then
if (NestLevel=0) then msgbox("Notify User")
return false
end if
Next
....
精彩评论