按下按钮时计时器不起作用(VB.NET)

时间:2015-06-26 19:59:32

标签: vb.net timer

我正在使用VB.NET在Visual Studio中单击一个按钮时编写一个Timer来启动。这是我的代码:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Timer1.Enabled = True
    Timer1.Start()
    seconds = +1
    Label2.Text = seconds.ToString

当程序运行并单击按钮时,计时器只移动一个整数,你必须继续按下它才能移动它。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

像这样使用StopWatch

Public Class Form1

    Private SW As New Stopwatch

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Timer1.Interval = 1000
        Timer1.Enabled = False
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        SW.Restart()
        Timer1.Start()
        UpdateStopwatch()
    End Sub

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

    Private Sub UpdateStopwatch()
        Label2.Text = SW.Elapsed.ToString("hh\:mm\:ss")
    End Sub

End Class