Ok first of all when I was reading a thread on Timers before this guy posted code for a timer and its really good, but only problem with it which I cant figure out is that when I run the program the Msgbox("You have run out of time")..Which is from Line 15 to 25 of Code...If anyone can amend the code (because I cant think of how to do it) so it just works normally and doesnt come up with the message box staright away, I would appreciate it.. Thanks

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartStop.Click
        If btnStartStop.Text = "Start" Then
            CountDownTime = Now.AddMinutes(span)
            Dim ts As TimeSpan = CountDownTime.Subtract(Now)
            Min.Text = ts.Minutes.ToString
            Sec.Text = ts.Seconds.ToString
            Timer1.Start()
            btnStartStop.Text = "Stop"
        Else
            Timer1.Stop()
            btnStartStop.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()
            MessageBox.Show("You have run out of time.")
            btnStartStop.Text = "Start"
        Else
            Dim ts As TimeSpan = CountDownTime.Subtract(Now)
            Min.Text = ts.Minutes.ToString
            Sec.Text = ts.Seconds.ToString
        End If
    End Sub

idk try this

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If CountDownTime = "0" Then
            Timer1.Stop()
            MessageBox("You have run out of time.")
            btnStartStop.Text = "Start"
        Else
            Dim ts As TimeSpan = CountDownTime.Subtract(Now)
            Min.Text = ts.Minutes.ToString
            Sec.Text = ts.Seconds.ToString
        End If

I am not sure what exactly your problem is.
the code below shows the messagebox after 1 minute. I guess you didn't set a value to span?

Private CountDownTime As Date

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartStop.Click
        If btnStartStop.Text = "Start" Then
            CountDownTime = Now.AddMinutes(1) 'changed the "span" to 1 for testing
            Dim ts As TimeSpan = CountDownTime.Subtract(Now)
            Min.Text = ts.Minutes.ToString
            Sec.Text = ts.Seconds.ToString
            Timer1.Start()
            btnStartStop.Text = "Stop"
        Else
            Timer1.Stop()
            btnStartStop.Text = "Start"
        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If CountDownTime < Now Then
            Timer1.Stop()
            MessageBox.Show("You have run out of time.")
            btnStartStop.Text = "Start"
        Else
            Dim ts As TimeSpan = CountDownTime.Subtract(Now)
            Min.Text = ts.Minutes.ToString
            Sec.Text = ts.Seconds.ToString
        End If
    End Sub

Thanks for the replys..but GeekByChoice Im still getting the same error....The Message Box saying "Youve run out of time" keeps popping up whenever I try and run the program, I only want it to come up when the time has finished its 60minutes.

then change CountDownTime = Now.AddMinutes(1) to CountDownTime = Now.AddMinutes(60)

Maybe you can post your load event?

No same problem, the Message Box pops up right at the beginning...What do you mean by load event?

could you attach the project here?
seems either you don't set a value for "span" or you have changed something to the timer in the designer already.

Hi TrueCoding try this routine
In Public Class add this code;

Dim StartTime As DateTime
Dim FinalSpan As TimeSpan
In   Private Sub Form1   add this code;
StartTime =DateTime.Now
Change   Private Sub Timer1   to this code;
Dim FinalSpan As TimeSpan = DateTime.Now.Subtract(StartTime) + InitialSpan
If FinalSpan.Hours = 1 Then
MessageBox.Show("the text you want")
Timer1.Stop()
End If
For your Start/Stop Button all you need is
Timer1.Start()
Timer1.Stop()

Hope this helps you out is simple but works gr. Gé

Hi,

I've tryed the code and works like it should, that means if you start then the counting is running until the time 60 sec. is passed and the messagebox will pop up. If you stop, the labels are showing the time left.

Public Class Form1
    Private CountDownTime As Date

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        btnStartStop.Text = "Start"
    End Sub

    Private Sub btnStartStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartStop.Click
        If btnStartStop.Text = "Start" Then
            CountDownTime = Now.AddMinutes(1) 'changed the "span" to 1 for testing            
            Dim ts As TimeSpan = CountDownTime.Subtract(Now)
            Min.Text = ts.Minutes.ToString
            Sec.Text = ts.Seconds.ToString
            Timer1.Start()
            btnStartStop.Text = "Stop"
        Else
            Timer1.Stop()
            btnStartStop.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()
            MessageBox.Show("You have run out of time.")
            btnStartStop.Text = "Start"
        Else

            Dim ts As TimeSpan = CountDownTime.Subtract(Now)
            Min.Text = ts.Minutes.ToString
            Sec.Text = ts.Seconds.ToString

        End If
    End Sub

End Class

Ok thanks Luc, but the message box keeps popping up straight away, but the message box does come up when 60 seconds is up.

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.