Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Countdown of timer 1

Status
Not open for further replies.

RonRepp

Technical User
Feb 25, 2005
1,031
US
Hi all:

I have a label (lStart) that I want to keep track of the time left for a Test that our employees have to take.

I'm not even getting it to display anything, but basically I have a starttime (sTime) and an end time (eTime). I want the label to show how much time is left.

This is what I have so far:
Code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim Ticks As Integer
        Ticks += 1
        If DateAndTime.TimeOfDay = Now Then
            MsgBox("Time is up!", MsgBoxStyle.Critical + MsgBoxStyle.OKOnly, Title)
        End If
        Dim TimeLeft As DateTime
        TimeLeft = DateAdd(DateInterval.Second, 1800 - Ticks, Now)
        lStart.Text = Format(TimeLeft, "MM:SS")

    End Sub

Any help will be greatly apreciated.


Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.

My newest novel: Wooden Warriors
 
Would something like this help:

Code:
	Private Duration As Integer = 30
	Private BaseDate As DateTime = New DateTime(1, 1, 1, 0, 0, 30, 0)

	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

		Label1.Text = BaseDate.ToLongTimeString
		Timer1.Interval = 1000
		Timer1.Start()

	End Sub

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

		Static Ticks As Integer = 0
		If Ticks = Duration Then
			Timer1.Enabled = False
			MessageBox.Show("Time's up!!")
		Else
			Ticks += 1
			Label1.Text = DateAdd(DateInterval.Second, -Ticks, BaseDate).ToLongTimeString
			Label1.Refresh()
		End If

	End Sub



[vampire][bat]
 
E & F:

Thanks for the quick reply. I'll try this and get back to the post.

Happy Easter.



Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.

My newest novel: Wooden Warriors
 
Ron, apologies, there was a bug in my code. Essentially, because of the use of the Static variable it would only run once.

Here is a better version with the timer set to 1 hour 5 minutes and 15 seconds.

Code:
	Private Duration As Integer = (1 * 3600) + (5 * 60) + 15
	Private BaseDate As DateTime = New DateTime(1, 1, 1, Duration \ 3600, (Duration Mod 3600) \ 60, Duration Mod 60, 0)
	Private Starting As Boolean = False

	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

		Label1.Text = BaseDate.ToLongTimeString
		Timer1.Interval = 1000
		Starting = True
		Timer1.Start()

	End Sub

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

		Static Ticks As Integer = 0
		If Starting Then
			Ticks = 0
			Starting = False
		End If
		If Ticks = Duration Then
			Timer1.Enabled = False
			MessageBox.Show("Time's up!!")
		Else
			Ticks += 1
			Label1.Text = DateAdd(DateInterval.Second, -Ticks, BaseDate).ToLongTimeString
			Label1.Refresh()
		End If

	End Sub

[vampire][bat]
 
Hi E & F:

The timer is counting from 12 am up. I must admit that I messed with your code a bit, but what I'm after is this:

Code:
'Mins=20 '(variable stored in DB)

Private Sub bStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bStart.Click
        bBack.Visible = True
        bBack.Enabled = True
        bNext.Visible = True
        bNext.Enabled = True
        pnlStart.Visible = False
        Timer1.Enabled = True
        'sTime = Now
        'eTime = DateAdd(DateInterval.Minute, 20, sTime)
        Duration = Mins
        BaseDate = New DateTime(1, 1, 1, Duration \ 3600, (Duration Mod 3600) \ 60, Duration Mod 60, 0)
        lTimeLeft.Text = BaseDate.ToLongTimeString
        Timer1.Interval = 1000
        Starting = True
        Timer1.Start()

    End Sub

I left the timer tick the same:

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

        Static Ticks As Integer = 0
        If Starting Then
            Ticks = 0
            Starting = False
        End If
        If Ticks = Duration Then
            Timer1.Enabled = False
            MessageBox.Show("Time's up!!")
        Else
            Ticks += 1
            lTimeLeft.Text = DateAdd(DateInterval.Second, -Ticks, BaseDate).ToLongTimeString
            lTimeLeft.Refresh()
        End If

    End Sub

What I'm trying to achieve is a countdown from Mins (20 in this case). The timer went off at 20 seconds instead of 20 minutes. I'm not a math expert, but I think that is correct according to your formula.

This is what I'm after:

[code--Pseudo]

mins = 20
sTime=12:00 am
eTime=12:20 am (stime + mins)

[/code]

when the timer ticks for 20 minutes, then the messagebox pops.

However, I want the label (lTimeLeft) to count down from 20 minutes in this format "MM:SS".

I'm sure that I wasn't clear enough, and I'll toy with your code, because I see that it's very close to what I need.

Thanks for your help...a star for you.





Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.

My newest novel: Wooden Warriors
 
Ron compare these:

[tt]Private Duration As Integer = (1 * 3600) + (5 * 60) + 15[/tt]

and

[tt]mins = 20
Duration = Mins[/tt]

In yours you are setting the duration to just 20 which will be 20 seconds, see the bold bit above - you needed to multiply by 60 to set the minutes.


Try this:

Code:
	Private Duration As Integer
	Private BaseDate As DateTime
	Private Starting As Boolean = False

	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click


		'****This allows for hours, minutes and seconds
		'Dim MyHours As Integer = 0
		'Dim MyMinutes As Integer = 20
		'Dim MySeconds As Integer = 0
		'BaseDate = New DateTime(1, 1, 1, MyHours, MyMinutes, MySeconds)
		'Duration = MyHours * 3600 + MyMinutes * 60 + MySeconds

		'****For just minutes i.e no hours or seconds then
		Dim MyMinutes As Integer = 20
		BaseDate = New DateTime(1, 1, 1, 0, MyMinutes, 0)
		Duration = MyMinutes * 60

		Label1.Text = BaseDate.ToString("mm:ss")
		Timer1.Interval = 1000
		Starting = True
		Timer1.Start()

	End Sub

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

		Static Ticks As Integer = 0
		If Starting Then
			Ticks = 0
			Starting = False
		End If
		If Ticks = Duration Then
			Timer1.Enabled = False
			MessageBox.Show("Time's up!!")
		Else
			Ticks += 1
			Label1.Text = DateAdd(DateInterval.Second, -Ticks, BaseDate).ToString("mm:ss")
			Label1.Refresh()
		End If

	End Sub


So basically, all that you didn't do was to multiply your minutes by 60 to get to seconds. Setting the TimerInterval property to 1000 means that the Tick event is fired every second.

Hope this helps.


[vampire][bat]
 
E & F:

Perfect!

Thanks so much. I'd give you another star if I could.

Have a great weekend.



Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.

My newest novel: Wooden Warriors
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top