Hello, i need help making a count down timer that can support at least 100 Hours, i got this code but it only supports 24 hours.

Dim TimePlayer1 As Date

    Private Sub Button1_MouseClick(sender As Object, e As MouseEventArgs) Handles Button1.MouseClick
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Select Case Me.Button1_Start.Text
                Case "Start"
                    Me.TimePlayer1 = Date.Now.AddMinutes((Me.TextHours.Text.ToString * 60) + (Me.TextMins.Text.ToString))

                    Me.Timer1.Start()
                    Me.Button1.Text = "Stop"
                Case "Stop"
                    Me.Timer1.Stop()
                    Me.Button1.Text = "Start"
            End Select
        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If Me.TimePlayer1 < Date.Now Then
            Me.Timer1.Stop()

            MessageBox.Show("Time just finished.", "Time Out", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            Dim remainingTime As TimeSpan = Me.TimePlayer1.Subtract(Date.Now)
            Me.LabelTime1Display.Text = "Time: " & String.Format("{0:d2}:{1:d2}:{2:d2}", remainingTime.Hours, remainingTime.Minutes, remainingTime.Seconds)
        End If
    End Sub

Any help ?

Recommended Answers

All 8 Replies

Hmmm... Never used these before but when you debug me.TimePlayer1 what do you get?

Is it the full date and time value or just a time value? I suspect it is passing just a time value and substituting the current date hence you are stuck in a 24 hour period.

hello !
please check the attach sample project of count down counter .
here is a code ,
i have three textboxes , txtsec , txtmint , txthours. and a timer here is a code .

    Dim sec As Int16
    Dim mint As Boolean
    'use this code at the timer 
     sec = sec + 1
        txtSec.Text = sec
        If sec = 60 Then
            txtmint.Text = Val(txtmint.Text) + 1
            If txtmint.Text = 60 Then
                mint = True
                txtmint.Text = 0
            End If

            If txtmint.Text = 0 And mint = True Then
                txthr.Text = Val(txthr.Text) - 1
                mint = False
            End If
            sec = 0
            txtSec.Text = 0
        End If

set your timer interval according to your equirement . 1000 is equal to 1 sec.

Regards

@G: Yea exactly that, it works perfectly and i have a countdown of example: 10:58:23, then after a minute and a few seconds it would look like: 10:53:10, etc.. it works but like you said im stuck with the 24Hours.

@waqas: im trying to do the countdown and show it in a label in this format: 00:00:00

just try to make some changes in code , where i am minus 1 from hr , use add . without efforts you cant learn anything.

regards

Hi, had the same problem, solved it with this piece of code. All it realy does is countdown seconds. The rest of it is just to keep the format 00:00:00 in the label where the countdown

Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
        Dim sec, min, hrs As String
        secondstotal -= 1
        If secondstotal < 0 Then
            secondstotal = 59
            minutestotal -= 1
        End If
        If minutestotal < 0 Then
            minutestotal = 59
            hourstotal -= 1
        End If
        If countdown = 0 Then
            Timer1.Stop()
        End If
        sec = secondstotal.ToString()
        min = minutestotal.ToString()
        hrs = hourstotal.ToString()
        If sec.Length = 1 Then
            sec = "0" + sec
        End If
        If min.Length = 1 Then
            min = "0" + min
        End If
        If hrs.Length = 1 Then
            hrs = "0" + hrs
        End If
        Label1.Text = hrs + ":" + min + ":" + sec
    End Sub

time is shown. As it is it wil countdown from 99:59:59. You can adapt it for more

Yea exactly that, it works perfectly and i have a countdown of example: 10:58:23, then after a minute and a few seconds it would look like: 10:53:10, etc.. it works but like you said im stuck with the 24Hours.

You will probably have to store your "target date and time" as a variable and on the tick event check if the date now is equal to the target date. This will allow you to go beyond the 24 hour period.

ok , i change my code little bit. please replace previous code with this one.

        sec = sec + 1
        txtSec.Text = sec
        If sec = 60 Then
            txtmint.Text = Val(txtmint.Text) + 1
            If txtmint.Text = 60 Then
                mint = True
                txtmint.Text = 0
            End If

            If txtmint.Text = 0 And mint = True Then
                txthr.Text = Val(txthr.Text) + 1
                mint = False
                If txthr.Text = 100 Then
                    Timer1.Enabled = False
                    txtSec.Text = 0


                End If
            End If
            sec = 0
            txtSec.Text = 0
        End If

Regards

Well i did it without any copy paste, since my original code could be easily changed by changing the computer's time i changed it to this:

Add 1 button, 1 timer, 1 label, and 2 textbox.

Rename 1 textbox to iHours_Text and the other to iMins_Text

This will be my declarations for the time:

    Dim iSecs As Integer = "0"
    Dim iMins As Integer = "0"
    Dim iHours As Integer = "0"

This is to add the hours, and the minutes and start the timer

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Me.iHours = iHours_Text.Text
        Me.iMins = iMins_Text.Text
        Me.Timer1.Start()
    End Sub

This will be my declarations to keep the 00:00:00 format:

    Dim iDisplaySecs As String = String.Empty
    Dim iDisplayMins As String = String.Empty
    Dim iDisplayHours As String = String.Empty

This will be to count down, let the 00:00:00 format and display to label:

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If Me.iSecs > "00" Then
            Me.iSecs -= 1
        ElseIf Me.iSecs <= "00" Then
            If Me.iMins > "00" Then
                Me.iMins -= 1
                Me.iSecs = "59"
            ElseIf Me.iMins <= "00" Then
                If Me.iHours > "00" Then
                    Me.iHours -= 1
                    Me.iMins = "59"
                    Me.iSecs = "59"
                End If
            End If
        End If

        If Me.iHours < "10".ToString Then
            Me.iDisplayHours = "0" & Me.iHours.ToString
        Else
            Me.iDisplayHours = Me.iHours.ToString
        End If

        If Me.iMins < "10" Then
            Me.iDisplayMins = "0" & Me.iMins.ToString
        Else
            Me.iDisplayMins = Me.iMins.ToString
        End If

        If Me.iSecs < "10" Then
            Me.iDisplaySecs = "0" & Me.iSecs.ToString
        Else
            Me.iDisplaySecs = Me.iSecs.ToString
        End If

        Label1.Text = Me.iDisplayHours & ":" & Me.iDisplayMins & ":" & Me.iDisplaySecs

        If (Me.iHours) + (Me.iMins) + (Me.iSecs) < "1" Then
            Timer1.Stop()
            MessageBox.Show("Time's up")
        End If
    End Sub
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.