Help please creating a timer form a Internet cafe

wicked14

Member
Joined
Feb 13, 2012
Messages
9
Programming Experience
Beginner
im trying to do is countdown timer.already done with the start time and stop time but when it comes to the pause time and resume cant seem to make it work, what i did was put on the event click on my pause button was add a timer1.enabled = false and on the resume button timer1.enabled true, it does it job pausing the running time, but when i resume for example, i pause at a time of 05:30 for 9 seconds then resume the timer resumes at 5:21, (im making a countdown timer here) ,what i want to happen is that, when i resume, it will resume from 5:31 then continue counting down to 5:30,5:29 . . . . an so on, please check my code. and hope u understand. thanks in advance..

Public Class Form1

Dim TimeEnd, TimeStart, TimeStop As New DateTime
Dim isStop, isPause As Boolean
Dim form02 As New Form2
Dim foo, TimePause, ElapseTime, TotalTimePaused As New TimeSpan
Dim h, m, s As Integer


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Enabled = False
TextBox1.Text = "0" : TextBox2.Text = "0" : TextBox3.Text = "0"

Button6.Hide()
End Sub


Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

If Timer1.Interval <> 100 Then Timer1.Interval = 100
If DateTime.Now > TimeEnd Then
TextBox5.Text = "Time End"
Timer1.Stop()

Else

If isStop = False And isPause = False Then
foo = TimeEnd - DateTime.Now
TextBox5.Text = String.Format("{0:D2}:{1:D2}:{2:D2}", _
foo.Hours, foo.Minutes, foo.Seconds)

End If
TextBox6.Text = TimeStart.ToString("hh:mm" & " " & "tt")

If isStop Then
TextBox4.Text = TimeStop.ToString("hh:mm" & " " & "tt")
Else

TextBox4.Text = TimeEnd.ToString("hh:mm" & " " & "tt")
End If
TextBox1.Text = "0" : TextBox2.Text = "0" : TextBox3.Text = "0"
End If

End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'START
Button1.Hide()
Timer1.Enabled = True
If Not Integer.TryParse(TextBox1.Text, h) OrElse _
Not Integer.TryParse(TextBox2.Text, m) OrElse _
Not Integer.TryParse(TextBox3.Text, s) Then
'bad input data
Exit Sub
End If
TimeEnd = DateTime.Now.AddHours(h).AddMinutes(m).AddSeconds( s)
TimeStart = DateTime.Now
TimeOfDay = DateTime.Now
Timer1.Interval = 1
Timer1.Start()
isStop = False

End Sub


Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'STOP
isStop = True
TimeStop = DateTime.Now


End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
isPause = True
Timer1.Enabled = False

Button1.Hide()
Button6.Show()
End Sub

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
'RESUME
If isPause = True Then
Timer1.Enabled = True
isPause = False
End If
End Sub
End Class
 
G'd evening Wicked,
Since your object's names and variables are meaningless to me, i'll just try and educated guess.
Your problem might be in the way you (don't) take advantage of the timer. Why are you ticking in fractions of seconds? wouldn't be easier to tick every second? in my opinion you have more variables of what you really need...
Any way, the following code just do that: count backwards. At the beginning i just tried to use your code and your objects, but i ended up cleaning a little bit and came up with this:
VB.NET:
 Private intElapsed As Integer
    Private intSeconds As Integer
    Private datTime As Date
    Private datCurrent As Date
    Private lngTicks As Long

    Private Sub frmTimer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call Reset()
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick
        Call ShowTime()
    End Sub

    Private Sub ShowTime()

        If Me.btnPause.Text = "Pause" Then
            lngTicks += 1
            datCurrent = DateAdd(DateInterval.Second, -lngTicks, datTime)
            Me.txtHours.Text = CStr(DatePart(DateInterval.Hour, datCurrent))
            Me.txtMinutes.Text = CStr(DatePart(DateInterval.Minute, datCurrent))
            Me.txtSeconds.Text = CStr(DatePart(DateInterval.Second, datCurrent))
        Else
            intElapsed += 1
            Me.txtElapsed.Text = intElapsed.ToString
        End If

    End Sub

    Private Sub btnPause_Click(sender As System.Object, e As System.EventArgs) Handles btnPause.Click
        Call PauseResume()
    End Sub

    Private Sub btnTimer_Click(sender As System.Object, e As System.EventArgs) Handles btnTimer.Click
        Call StartStop()
    End Sub
    Private Sub StartStop()
        If Me.btnTimer.Text = "Start" Then
            Me.btnTimer.Text = "Stop"
            datTime = DateAdd(DateInterval.Minute, Me.txtRented.Value, Date.Now)
            Me.txtEnd.Text = datTime.ToLongTimeString
            If Not Me.Timer1.Enabled Then Me.Timer1.Enabled = True
        Else
            Me.btnTimer.Text = "Start"
            Call Reset()
        End If
    End Sub

    Private Sub PauseResume()
        If Me.btnPause.Text = "Pause" Then
            Me.btnPause.Text = "Resume"
        Else
            Me.btnPause.Text = "Pause"
        End If
    End Sub


    Private Sub Reset()
        Me.txtHours.Text = "0" : Me.txtMinutes.Text = "0" : Me.txtSeconds.Text = "0"
        Me.Timer1.Enabled = False
        intElapsed = 0
        intSeconds = 0
        Me.txtRented.Value = 30
        Me.txtEnd.Text = String.Empty
    End Sub

  
End Class
Before you start reading the code here is brief explanation:
The application expect just one Value [The time in minutes] that is for a the rent of a given equipment. This value will be set manually in the txtRent, which is a NumericUpDown box with intervals of 30.
The timer control is preset to 1000 milliseconds interval and enabled just once as well as stopped. The buttons change their caption on each click to behave as boolean variables.

As you can see, the code that actually solves the problem is the "ShowTime" Sub and the two numeric variables lngTicks and intElepased. The rest of the code is just as plain and simple as could be.

Good luck
 
Last edited:
Up for this thread i need an example program code like this thank you . :)

Isn't that exactly what this thread already provides? Like so many people, I don't think you actually use the term "example" correctly. It sounds like what you want is something already works that you can simply copy and paste. If you have a specific question about the contents of this thread then please ask it. If you have a question that doesn't relate specifically to the topic of this thread, i.e. pausing a countdown timer, then please create a new thread for your new topic.

The answer to the original question is actually quite simple: use the Stopwatch class. Just like a real stopwatch, it supports pausing. Let's say that you want to allow something to run for 10 minutes. You would create a TimeSpan that represented 10 minutes and then Start a Stopwatch. At any point you can subtract the Elapsed property of the Stopwatch from your original TimeSpan and the result will give you the time remaining. You can Stop and Start the Stopwatch as often as you like and that calculation will always be correct.
 
Back
Top