Timer to count down in VB.Net 2010?

2019-04-14 09:17发布

I'm trying to use a timer to count down from a specified time I choose with the time being separated into minutes and seconds using the format MM:SS and then stop when the time reaches 00:00.

So far I've used a previous answer that was found on here and modified it to the best of my knowledge with counting down although I've hit a snag in which when the timer successfully starts counting down, it's delayed and out of sync when counting down the minutes.

For example, counting down from 120 seconds;

02:00 > 02:59 > 02:58 > 02:57 > 02:56 > 02:55

And then when continuing to count down past 90 seconds under the same test;

02:30 > 01:29 > 01:28 > 01:27 > 01:26 > 01:25

When the countdown reaches 00 or 30 seconds, it incorrectly displays the minutes left and can't understand or figure out how to fix it.

Here is my code for my Counting Timer;

Private Sub tmrCountdown_Tick(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) _
                          Handles tmrCountdown.Tick

    SetTime = SetTime - 1
    lblTime.Text = FormatTime(SetTime)

    If SetTime = 0 Then
        tmrCountdown.Enabled = False
    End If

End Sub

Here is my code for the Function formatting the time;

Public Function FormatTime(ByVal Time As Integer) As String
    Dim Min As Integer
    Dim Sec As Integer

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

    'Seconds
    Sec = Time Mod 60

    Return Format(Min, "00") & ":" & Format(Sec, "00")

End Function

And here is my code for the Form Load;

Private Sub frmSinglePlayer_Load(ByVal sender As System.Object, _
                                 ByVal e As System.EventArgs) _
                             Handles MyBase.Load

    'Setting the time.
    SetTime = 120
    lblTime.Text = FormatTime(SetTime)
    tmrCountdown.Enabled = True

End Sub

I've set;

    Dim SetTime As Integer

At the top of my Public Class so I am able to input a specified time into the countdown timer. This is probably something incredibly silly and I can't figure out what it is.

Any help is greatly appreciated and please bare in mind, I am a beginner at programming and get easily confused with large walls of code. (I can barely understand the Function as it is.)

Thank you for helping!

5条回答
狗以群分
2楼-- · 2019-04-14 09:53

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
查看更多
叼着烟拽天下
3楼-- · 2019-04-14 09:59

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
查看更多
Root(大扎)
4楼-- · 2019-04-14 09:59

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
查看更多
看我几分像从前
5楼-- · 2019-04-14 10:03

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
查看更多
疯言疯语
6楼-- · 2019-04-14 10:05

More simply:

Format((Math.Floor(lSeconds / 60)), "00") & ":" & Format((lSeconds Mod 60), "00")
查看更多
登录 后发表回答