我正在使用vb.net,我遇到这种情况:
我有一个按钮(Mybutton1)。
我还有一个包含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循环的每一步中,当条件为真时,按钮变为启用状态,在这种情况下,我希望此循环停止,直到用户按下Button。 (按钮的点击事件,最后有一些说明,使按钮再次被禁用)
答案 0 :(得分:0)
Exit For
将突破for循环
.....
For I=1 To nr1
{.... some instructions...}
If condition1 Then
Mybutton1.enable=true
Exit For
End If
Next
答案 1 :(得分:0)
如果您希望在用户单击按钮后能够恢复循环,则可以通过将其声明为类级别字段来保存循环变量(I
在代码中)的当前值。当用户单击Button1时,以下代码启动循环。只要someCondition
评估为True
,它就会退出。每次用户单击Button1时,循环都会从停止的位置继续。如果用户在循环结束后单击Button1,则会显示错误消息。
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