如何在计时器滴答事件vb 2015中进行循环?

时间:2016-12-21 07:42:30

标签: vb.net visual-studio timer

我想在计时器中做For Loop。因为我想连续检查一个带有元素row的数组而且我不能继续,因为循环似乎没有从0开始移动,有没有办法解决这个问题?

我首先尝试过这个:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
For row = 0 To 9
msgbox(row)
Next

然后我尝试了另一种方法,如答案所示

 Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
row+=1
msgbox(row)
if row = 10 then
timer1.stop()
end if
Next

MsgBox现在将在刻度线上输出0 ++,但不会在10点停止。

Output Picture

2 个答案:

答案 0 :(得分:0)

而不是使用for循环使用全局变量并在tick函数内增加它

Dim ctr as integer = 0

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

ctr=ctr+1
row(ctr)
msgbox(row)


if ctr == 10 then
ctr = 0
timer1.stop
end if

答案 1 :(得分:0)

Dim row As Integer = -1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    RefreshData()
    Timer1.Enabled = True

    initialize()
End sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    increment()
    MsgBox(row)
End sub
Private Sub increment()
    If row > 8 Then
        row = 0

    Else
        row = row + 1

    End If

'my if else statement for checking array variable(row) 
End Sub

使用此代码,msgbox能够输出0到9并重复该过程,因为我需要它来连续监视阵列。出于某种原因,虽然当msgbox位于增量方法的第一行或者在调用增量之前的定时器滴答时,输出保持-1整个时间不知道为什么。无论如何,感谢所有的输入,因为我仍然是视觉基础2015的新手