如何从递归循环退出中退出
如何从下面的代码中退出递归循环。我想通知最终用户在退出循环之前选择 msgBox 中的复选框。谢谢。
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种方法是将 PrintRecursive 更改为返回布尔值的函数,其中 true 表示“停止”
然后更改递归调用以检查返回值。
不过,请注意,退出递归时,消息框将在每个嵌套级别显示。为了避免这种情况,您可以将嵌套级别的参数添加到 PrintRecursive,以便您可以知道何时位于顶层。
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.
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.