I'm using a timer which counts up on a button click when i press it, and when it reaches the time i desire, usually around the 45 minute mark i click my other button which stops the timer and the time remains static, so if i click my button to stop and my label says "45:34" then it will just stay like that as a static label. This is fine, i have done all this with no problem. The problem i have though is being able to resume this timer but start it from "45:00" instead of "00:00"

Dim starttime As DateTime

'Timer settings'

Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
        Dim timedifference As TimeSpan = DateTime.Now.Subtract(starttime)
        Dim newdate As DateTime = timedifference.ToString
        Label7.Text = newdate.ToString("mm:ss")
    End Sub

    'Stops/pauses the timer'

Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
        Timer2.Stop() 'This is my other timer, not related to this'
        Timer3.Stop() 'This is the timer which controls the label ie the ticking minutes'
        Label8.Text = "Match Paused..."
        Button7.Enabled = True
    End Sub

    'When i click this button the timer starts counting from 00:00'

    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        Timer3.Start()
        starttime = DateTime.Now

When i click a seperate button, let's say button9 for example, the timer should reset to "45:00" and start counting up in label7 just like it did before.

I believe it involves part of this code

starttime = DateTime.Now

But instead of 'Now' it would be something else?

Recommended Answers

All 10 Replies

Dim timedifference As TimeSpan = DateTime.Now.AddMinutes("45").Subtract(starttime)

Thanks for the reply.

I add

Dim timedifference As TimeSpan

In the public class, but where do i add the rest of the code? Not sure if i'm reading it right.

Is this not right

Private Sub Button14_Click(sender As Object, e As EventArgs) Handles Button14.Click
        Timer3.Start()
        timedifference = DateTime.Now.AddMinutes("45").Subtract(starttime)

Do i need to make a new timer? I think i do.

in the timer tick event, you can also try if statement

    Private Sub Timer3_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer3.Tick
        If label7.text >= "45:00" Then
            Dim timedifference As TimeSpan = DateTime.Now.AddMinutes("45").Subtract(starttime)
            Dim newdate As DateTime = timedifference.ToString
            Label7.Text = newdate.ToString("mm:ss")
        Else
            Dim timedifference As TimeSpan = DateTime.Now.Subtract(starttime)
            Dim newdate As DateTime = timedifference.ToString
            Label7.Text = newdate.ToString("mm:ss")
        End If
    End Sub

When i try that code in a new timer i get the error "Additional information: Conversion from string "735323.13:18:46.3192731" to type 'Date' is not valid."

My original code for the timer3 which counts from 00:00 works fine but when i tried the last code you posted it to count from 45:00 it crashes.

Dim timedifference As TimeSpan = DateTime.Now.Subtract(starttime)
        Dim newdate As DateTime = timedifference.ToString
        Label7.Text = newdate.ToString("mm:ss")

I get it now. Thanks for your help. Managed to get it working.

Is there a way to pause the clock at all? and then have a button to resume the clock at the time which was last?

So this adds time on, but what about to resume?
I know the original question was answered but this is useful to me too.

Dim timedifference As TimeSpan = DateTime.Now.AddMinutes("45").Subtract(starttime)

Thanks for your help with my threads.

Resume Button

    Dim minutes As String
    Dim seconds As String

    Private Sub ResumeTime_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ResumeTime.Click
        minutes = Label7.Text.Substring(0, 2)
        seconds = Label7.Text.Replace(minutes & ":", "")
        Timer3.Start()
    End Sub

Tick event

    Private Sub Timer3_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer3.Tick
        Dim timedifference As TimeSpan = DateTime.Now.AddMinutes(minutes).AddSeconds(seconds).Subtract(starttime)
        Dim newdate As DateTime = timedifference.ToString
        Label7.Text = newdate.ToString("mm:ss")
    End Sub

Thanks, i'll check that out later.

I've come into a bit of a problem. The timer only goes up to 60 minutes and then restarts. Is there anything i should change to make it go to 100, or 200 even?

since this thread is solved, daniweb members will visit this thread rarely so i recomand you start a new one. and i dont know the answer to your last question.
good luck

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.