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

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.