Timer to count down in VB.Net 2010?

限于喜欢 提交于 2019-12-04 17:57:34

Play with this:

Public Class frmSinglePlayer

    Private TargetDT As DateTime
    Private CountDownFrom As TimeSpan = TimeSpan.FromMinutes(3)

    Private Sub frmSinglePlayer_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        tmrCountdown.Interval = 500
        TargetDT = DateTime.Now.Add(CountDownFrom)
        tmrCountdown.Start()
    End Sub

    Private Sub tmrCountdown_Tick(sender As Object, e As System.EventArgs) Handles tmrCountdown.Tick
        Dim ts As TimeSpan = TargetDT.Subtract(DateTime.Now)
        If ts.TotalMilliseconds > 0 Then
            lblTime.Text = ts.ToString("mm\:ss")
        Else
            lblTime.Text = "00:00"
            tmrCountdown.Stop()
            MessageBox.Show("Done")
        End If
    End Sub

End Class

Take a tested sample of countdown timer. Make the changes you need(ex the format of the time).

Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        SetTime = 70

        AddHandler dtTimer.Tick, AddressOf dtTimer_Tick
        dtTimer.Interval = New TimeSpan(0, 0, 1)
        dtTimer.Start()
    End Sub

    Private Property SetTime As Integer

    Private Sub dtTimer_Tick(sender As Object, e As EventArgs)

        Dim iMinutes As Integer
        Dim iSeconds As Integer

        If SetTime = 0 Then
            dtTimer.Stop()
            txtTime.Text = "0:0"
            Exit Sub
        End If

        SetTime -= 1
        iMinutes = Math.Floor(SetTime / 60)
        iSeconds = SetTime Mod 60

        txtTime.Text = iMinutes & ":" & iSeconds
    End Sub

Try this

'the amount of time to countdown from
Dim countDownFrom As New TimeSpan(0, 0, 10) 'ten seconds
'a Stopwatch to track how long running
Dim stpw As New Stopwatch

Private Sub Button1_Click(sender As Object, _
                          e As EventArgs) Handles Button1.Click
    Timer1.Interval = 250 'how often to update display
    Timer1.Start() 'start the display updater
    stpw.Reset() 'restart the stopwatch
    stpw.Start()
    'or depending on version of .Net
    'stpw.Restart
End Sub

Private Sub Timer1_Tick(sender As Object, _
                        e As EventArgs) Handles Timer1.Tick
    If stpw.Elapsed <= countDownFrom Then
        Dim toGo As TimeSpan = countDownFrom - stpw.Elapsed
        lblTime.Text = String.Format("{0:00}:{1:00}:{2:00}", toGo.Hours, toGo.Minutes, toGo.Seconds)
    Else
        Timer1.Stop()
        stpw.Stop()
    End If
End Sub

your mistake in your original code is that you are using the MOD operater incorrectly.

   'Minutes
    Min = ((Time - Sec) / 60) Mod 60

    'Seconds
    Sec = Time Mod 60

At 2:00 you see 2:00 because:

Min = ((120-00) / 60 ) MOD 60 = 2 MOD 60 = 2

Sec = 120 MOD 60 = 0

At 1:59 you see 2:59 because

Min = (119 / 60) MOD 60 = 1.98 MOD 60 = 1.98 = 2

Sec = 119 MOD 60 = 59

After 31 seconds your minutes changes from 2 to 1 because

Min = (89 / 60) MOD 60 = 1.48 MOD 60 = 1.48 = 1
Julien

More simply:

Format((Math.Floor(lSeconds / 60)), "00") & ":" & Format((lSeconds Mod 60), "00")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!