Timer di没有按照我的意愿正确停止

时间:2016-10-26 08:05:47

标签: vb.net timer

所以我希望我的定时器在“i”(i As Integer = 0)达到90时停止。但是当我测试它时,定时器在“i”达到100时停止。或者当“i”为90时定时器停止,但“i”更改为100.如何解决问题? 如果你对我的解释感到困惑,那就是代码

Dim i As Integer = 0
Private Sub PictureBox1_MouseHover(sender As Object, e As EventArgs) Handles PictureBox1.MouseHover
    Form3.Show() '<<You can ignore this
    Timer2.Start() '<<You can ignore this
    Timer4.Start()
End Sub

Private Sub PictureBox1_MouseLeave(sender As Object, e As EventArgs) Handles PictureBox1.MouseLeave
    Timer2.Stop() '<<You can ignore this
    Timer3.Start() '<<You can ignore this
    Timer4.Stop()
    Timer5.Start()
End Sub

Private Sub Timer4_Tick(sender As Object, e As EventArgs) Handles Timer4.Tick
    If i = "90" Then
        Timer4.Stop()
    End If
    i = i + 10
    PictureBox1.Invalidate()
    TextBox2.Text = i '<< I want to check the "i"
End Sub
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
    With e.Graphics
        .TranslateTransform(PictureBox1.Width \ 2, PictureBox1.Height \ 2)
        .RotateTransform(i)
        .DrawImage(Image.FromFile("D:\Gambar Desktop\RodMerah.png"), -(PictureBox1.Width \ 2), (-PictureBox1.Height \ 2))
    End With
End Sub

Private Sub Timer5_Tick(sender As Object, e As EventArgs) Handles Timer5.Tick
    If i = 0 Then
        Timer5.Stop()
    End If
    i = i - 10
    PictureBox1.Invalidate()
End Sub

1 个答案:

答案 0 :(得分:1)

实际上我只需要在If之后添加i = i + 10。所以来自

Private Sub Timer4_Tick(sender As Object, e As EventArgs) Handles Timer4.Tick
    If i = "90" Then
        Timer4.Stop()
    End If
    i = i + 10
    PictureBox1.Invalidate()
    TextBox2.Text = i '<< I want to check the "i"
End Sub

我把它改成了

Private Sub Timer4_Tick(sender As Object, e As EventArgs) Handles Timer4.Tick
    i = i + 15
    PictureBox1.Invalidate()
    TextBox2.Text = i '<< I want to check the "i"
    If i = "90" Then
        Timer4.Stop()
    End If
End Sub

但如果你将Else i = i + 10添加到第二个,但仍然有效,但没有什么不同。