Create a simple timer to count seconds, minutes and hours

后端 未结 4 911
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-07 15:47

I\'m trying to create a pretty simple program that basically is a timer. I have three sets of labels, lbl_seconds, lbl_minutes and lbl_hours

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-07 16:15

    Here is an example of this

    Dim timercount As Integer = 60 'The number of seconds 
    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        Timer1.Interval = 1000 'The number of miliseconds in a second
        Timer1.Enabled = True 'Start the timer
    End Sub
    
    Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
        Timer1.Enabled = False 'Stop the timer
        timercount = 60 'Reset to 60 seconds
        lblOutput.Text = timercount.ToString() 'Reset the output display to 60
    End Sub
    
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        lblOutput.Text = timercount.ToString() 'show the countdown in the label
        If timercount = 0 Then 'Check to see if it has reached 0, if yes then stop timer and display done
            Timer1.Enabled = False
            lblOutput.Text = "Done"
        Else 'If timercount is higher then 0 then subtract one from it
            timercount -= 1
        End If
    End Sub
    

提交回复
热议问题