Hi,

I am requesting code for a countdown timer please, where 2 labels (Minutes label = label6 and Seconds label = label7)

I have tried for a long time to try and get this to work, and it has to be "Hours:Minute" format.
I will add below the awful start I have made

If Label7.Text = "1" Then
            Label6.Text = (Label6.Text) - Val(1)
        ElseIf Label7.Text = "1" Then
            Label7.Text = "59"
        ElseIf Label7.Text <= 60 Then
            Label7.Text = (Label7.Text) - Val(1)
        ElseIf Label6.Text = "0" And Label7.Text = "59" Then
            Clock2.Stop()
            Label7.Text = "0"
        End If

There is lots of bugs, such as the last minute is cut out, and it is just buggy, the user must be able to select a time/duration for the timer to work, and also this must be able to change every time the time reaches 00:00, so if someone could help me i would really appreciate it,

Thank you in advance,

Luke

Dani AI

Generated

The original issue from comes from treating label text as the timer state and doing string comparisons/arithmetics (for example comparing Label7.Text to "1" and trying to subtract from a string). Two robust patterns work well here: keep an absolute end time (DateTime/TimeSpan) or keep a single numeric remaining-seconds counter. For a user-selectable duration and an easy "Hours:Minute" display, the remaining-seconds approach is simplest and avoids roll-over bugs and string math.

Example (new, compact approach — assumes NumericUpDown controls nudHours/nudMinutes, a Timer1 set to 1000 ms, a display label lblTime, and a Start/Stop button btnStart):

Private remainingSeconds As Integer = 0

Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
    If Timer1.Enabled Then
        Timer1.Stop() : btnStart.Text = "Start" : Return
    End If
    remainingSeconds = CInt(nudHours.Value) * 3600 + CInt(nudMinutes.Value) * 60
    If remainingSeconds <= 0 Then Return
    UpdateDisplay()
    Timer1.Interval = 1000
    Timer1.Start()
    btnStart.Text = "Stop"
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    remainingSeconds -= 1
    If remainingSeconds <= 0 Then
        Timer1.Stop()
        remainingSeconds = 0
        System.Media.SystemSounds.Beep.Play()
        btnStart.Text = "Start"
    End If
    UpdateDisplay()
End Sub

Private Sub UpdateDisplay()
    Dim hrs = remainingSeconds \ 3600
    Dim mins = (remainingSeconds Mod 3600) \ 60
    lblTime.Text = String.Format("{0}:{1:00}", hrs, mins)  ' Hours:Minute
End Sub

Notes and troubleshooting: parse input with TryParse or use NumericUpDown to avoid conversion errors. Keep Timer interval at 1000 ms for second resolution; for high precision compare DateTime.Now against a stored end time instead (avoids drift). On completion, stop the timer, play a sound/message, and re-enable inputs so a new duration can be set each time it hits 00:00.

This ties to / (DateTime/TimeSpan is valid for absolute deadlines) and to (simple counter + start/stop). The provided pattern removes string-based math, shows Hours:Minute formatting, and makes changing the duration after each run straightforward.

Recommended Answers

All 5 Replies

Hi Luke,
Instead of converting Label to Integer (Label1 to Minutes), Use DateTime Class for Date or Time operations.

It has methods and Properties to manipulate it.

Try this:

Public Class Form1
    Private CountDownTime As DateTime
    Private span As Integer = 1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Button1.Text = "Start" Then
            CountDownTime = Now.AddMinutes(span)
            Dim ts As TimeSpan = CountDownTime.Subtract(Now)
            labMin.Text = ts.Minutes.ToString
            labSec.Text = ts.Seconds.ToString
            Timer1.Start()
            Button1.Text = "Stop"
        Else
            Timer1.Stop()
            Button1.Text = "Start"
        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If CountDownTime < Now Then
            Timer1.Stop()
            My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Beep)
            MessageBox.Show("You Lose!")
            Button1.Text = "Start"
        Else
            Dim ts As TimeSpan = CountDownTime.Subtract(Now)
            labMin.Text = ts.Minutes.ToString
            labSec.Text = ts.Seconds.ToString
        End If
    End Sub
End Class

how do i add a date to count down from instead of minutes or seconds?

Try this:

Public Class Form1
    Private CountDownTime As DateTime
    Private span As Integer = 1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Button1.Text = "Start" Then
            CountDownTime = Now.AddMinutes(span)
            Dim ts As TimeSpan = CountDownTime.Subtract(Now)
            labMin.Text = ts.Minutes.ToString
            labSec.Text = ts.Seconds.ToString
            Timer1.Start()
            Button1.Text = "Stop"
        Else
            Timer1.Stop()
            Button1.Text = "Start"
        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If CountDownTime < Now Then
            Timer1.Stop()
            My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Beep)
            MessageBox.Show("You Lose!")
            Button1.Text = "Start"
        Else
            Dim ts As TimeSpan = CountDownTime.Subtract(Now)
            labMin.Text = ts.Minutes.ToString
            labSec.Text = ts.Seconds.ToString
        End If
    End Sub
End Class

hae im trying to make a countdown timer can you tell me hoe to stop the time thanks
Hawre Eliassi

hae im trying to make a countdown timer can you tell me hoe to stop the time thanks
Hawre Eliassi

Public Class Form1

    Private myCounter As Integer = 60

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Interval = 100 '// tick every 1/10th of a second.
        Button1.Text = "Start"
        Me.Text = myCounter
    End Sub

    '// start and stop the timer.
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Timer1.Enabled = False Then
            Timer1.Start()
            Button1.Text = "Stop"
        ElseIf Timer1.Enabled = True Then
            Timer1.Stop()
            Button1.Text = "Start"
        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If myCounter = 0 Then
            Timer1.Stop() '// stop the timer.
            MsgBox("All Done.", MsgBoxStyle.Information)
            myCounter = 60 '// reset the timer.
            Button1.Text = "Start"
            Me.Text = myCounter
            Exit Sub '// skip the remaining code.
        End If
        myCounter -= 1 '// subtract 1 from the counter.
        Me.Text = myCounter
    End Sub

End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.