Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 继续下一步中的一个for。。。下一个循环,仅当用户单击按钮时_Vb.net - Fatal编程技术网

Vb.net 继续下一步中的一个for。。。下一个循环,仅当用户单击按钮时

Vb.net 继续下一步中的一个for。。。下一个循环,仅当用户单击按钮时,vb.net,Vb.net,我正在使用vb.net,我遇到了以下情况: 我有一个按钮,我的按钮 我还有一个包含for..next循环的子循环: ..... for I=1 to nr1 {.... some instructions...} if condition1 then Mybutton1.enable=true !!!!! at this point the loop must wait for the user to press the button !!!!!

我正在使用vb.net,我遇到了以下情况:

我有一个按钮,我的按钮

我还有一个包含for..next循环的子循环:

.....
for I=1 to nr1 
    {.... some instructions...}
   if condition1 then  
       Mybutton1.enable=true
       !!!!! at this point the loop must wait for the user to press the button !!!!!
   end if
Next
在这个For循环的每一步中,当一个条件为真时,按钮就被启用,在这种情况下,我希望这个循环停止,直到用户按下按钮。按钮的点击事件,在结尾有一些指令,使按钮再次被禁用

退出For将中断For循环

.....
For I=1 To nr1 
    {.... some instructions...}
   If condition1 Then  
       Mybutton1.enable=true
       Exit For
   End If
Next

如果希望在用户单击按钮后能够恢复循环,可以将循环变量I的当前值声明为类级字段,从而将其保存在代码中。以下代码在用户单击按钮1时启动循环。只要某个条件的计算结果为True,它就会退出。每次用户单击按钮1时,循环将从其停止的位置继续。如果用户在循环完成后单击按钮1,将显示错误消息

Public Class Form1
    Private number As Integer = 0
    Private lastNumber As Integer = 4

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If number >= lastNumber Then
            MessageBox.Show("Finished")
        End If
        Do While number < lastNumber
            number += 1
            Dim somecondition As Boolean
            'code that sets someCondition
            If somecondition Then Exit Sub
        Loop
    End Sub
End Class

我已经编辑了你的标题。请看,如果共识是否定的,他们就不应该这样做。与其在满足条件的每一步都打破for循环,为什么不使用do或while循环呢?但我想要的是,循环在条件为真时停止,并在用户按下按钮时继续下一步。我如何使用While循环来实现这一点呢?您所描述的就是所谓的协程。您希望一个方法在其他任务完成之前一直执行。在本例中,按下按钮,然后恢复执行,其上下文/状态与以前相同。这不是小事,但肯定是可能的。对不起,但是。。下一个循环不应在按钮内;s单击事件。这个循环是在另一个子里面的,还有按钮;s click事件包含其他代码,这些代码不仅在for循环中执行,甚至在用户单击按钮的其他情况下也会执行。我只是尽可能地简化代码。您可以将代码移动到单独的子项中。只需保持相同的逻辑:循环变量在表单级别声明,子项在满足条件时退出,子项在您想要恢复处理时再次调用。我不能在按钮内调用子项;s click事件,因为按钮的click事件用于与循环无关的其他情况。@alex:我的意思是,只要你想调用它,就随时调用它。对不起,我不明白你的意思。你能修改你的代码来表达你的想法吗?非常感谢。好的,但是如果我退出for循环,当用户按下按钮时,我怎么能再次进入呢?实际上,我想的解决方案不起作用,所以我将其删除。我想您可以使用AutoResetEvent,但是由于表单使用单个线程运行,因此它会阻止并冻结整个表单,而不是我想要的。