如何从递归循环退出中退出

发布于 2024-09-06 08:57:53 字数 734 浏览 1 评论 0原文

如何从下面的代码中退出递归循环。我想通知最终用户在退出循环之前选择 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生死何惧 2024-09-13 08:57:53

一种方法是将 PrintRecursive 更改为返回布尔值的函数,其中 true 表示“停止”

然后更改递归调用以检查返回值。

For Each aNode In n.Nodes
    if not PrintRecursive(aNode) then 
        msgbox("Notify User")
        return false
    end if
Next

不过,请注意,退出递归时,消息框将在每个嵌套级别显示。为了避免这种情况,您可以将嵌套级别的参数添加到 PrintRecursive,以便您可以知道何时位于顶层。

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
....

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
....
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文