Search code examples
vb.nettimercountdowntimer

Timer to count down in VB.Net 2010?


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!


Solution

  • 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