yet another timer question

timer

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1648 Views - Last Post: 23 December 2008 - 09:50 PM Rate Topic: -----

#1 Darkangler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 09-September 08

yet another timer question

Posted 22 December 2008 - 08:00 PM

ok... I want to display a 30 second timer in a Label and have it update every second to show the user the amount of time he/she has left on the game.

	Dim Time As Double = 0.3
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		lblTime.Text = Time
	End Sub

	Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
		Dim i As Integer = 0
		lblTime.Text = Time
		For i = 1 To 30
			lblTime.Text -= 0.01
			Threading.Thread.Sleep(100)
		Next
	End Sub


It loads, but then goes right to zero... I have the timer set to update every 1000 milliseconds in the properties menu for the timer, but it just blinked and it was over the first run without the Threading.Thread.Sleep.. I tried 1000, but it froze, I tried 10 and it blinked... I try 100 and it freezes, so I'm at a standstill with the timer for now.

Is This A Good Question/Topic? 0
  • +

Replies To: yet another timer question

#2 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: yet another timer question

Posted 22 December 2008 - 08:25 PM

The problem is that you initialize the variable (set its value to 0) every second. You should declare and initialize the i variable outside the Timer1_Tick event. And you don't need the Thread.Sleep here. Just set the Interval property for the Timer to 1000.

This post has been edited by Core: 22 December 2008 - 08:26 PM

Was This Post Helpful? 0
  • +
  • -

#3 Darkangler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 09-September 08

Re: yet another timer question

Posted 22 December 2008 - 08:43 PM

View PostCore, on 22 Dec, 2008 - 07:25 PM, said:

The problem is that you initialize the variable (set its value to 0) every second. You should declare and initialize the i variable outside the Timer1_Tick event. And you don't need the Thread.Sleep here. Just set the Interval property for the Timer to 1000.


I set the variable to 0.3, the i for the If Statement was set to zero to initialize the count..And without the Thread.Sleep it just blinks and it is finished... I don't entirely understand what you mean by your response, maybe that's the misunderstanding...
Was This Post Helpful? 0
  • +
  • -

#4 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: yet another timer question

Posted 22 December 2008 - 08:54 PM

You should have this variable initialization statement outside the Timer_Tick event:
Dim i As Integer = 0



So your variable should have the public scope and declared, for example, right before any event declarations. It will look like this
Public i As Integer


Then, set your timer Interval property to 10000 (10 seconds) and make your Timer1_Tick event look like this:

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
		Time -= 0.01
		lblTime.Text = Time
End Sub


This post has been edited by Core: 22 December 2008 - 08:58 PM

Was This Post Helpful? 0
  • +
  • -

#5 Darkangler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 09-September 08

Re: yet another timer question

Posted 22 December 2008 - 09:02 PM

View PostCore, on 22 Dec, 2008 - 07:54 PM, said:

You should have this variable initialization statement outside the Timer_Tick event:
Dim i As Integer = 0



So your variable should have the public scope and declared, for example, right before any event declarations. It will look like this
Public i As Integer


Then, set your timer Interval property to 10000 (10 seconds) and make your Timer1_Tick event look like this:

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
		Time -= 0.01
		lblTime.Text = Time
End Sub



Well, now it's updating every second... thanks for that much of it... but when I have
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
		Time -= 0.01
		lblTime.Text = Time
End Sub

It gives me a strange negative number then E+16 afterward...
Was This Post Helpful? 0
  • +
  • -

#6 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: yet another timer question

Posted 22 December 2008 - 09:19 PM

You just have to add a 'end of time' verification, so your Timer1_Tick event looks like this:

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Not Time =0
		Time -= 0.01
		lblTime.Text = Time
Else
		Timer1.Enabled = False
End If
End Sub



In this way you check if the time did not reach the end limit.
Was This Post Helpful? 0
  • +
  • -

#7 Darkangler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 09-September 08

Re: yet another timer question

Posted 22 December 2008 - 09:21 PM

wow... thanks man... that works perfectly! :):):)
Was This Post Helpful? 0
  • +
  • -

#8 Darkangler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 09-September 08

Re: yet another timer question

Posted 23 December 2008 - 08:39 PM

I have the code just as you had it, but it continued to move after zero and even went as far as to go to 0.09999999999999 after 0.1 and continued to drop with the several nine's after the number that should have been subtracted. After the code hit 0.009999999999 it went to negative numbers..
Was This Post Helpful? 0
  • +
  • -

#9 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: yet another timer question

Posted 23 December 2008 - 09:03 PM

Yeah. Why do you want to count seconds as decimals? I would recommend counting seconds as integers, not decimals. Well, it's your choice. The code for your case would be like this:

If Time > 0.009 Then
		Time -= 0.01
		lblTime.Text = Format(Time, "0.##")
Else
		Timer1.Enabled = False
End If



I added the Format part to eliminate the showing of long decimals (like you mentioned, 0.09999999999). It will just show it like 0.09. That is completely optional.
Was This Post Helpful? 0
  • +
  • -

#10 Darkangler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 09-September 08

Re: yet another timer question

Posted 23 December 2008 - 09:12 PM

Is there a way to reset the timer in the mnuMouseReset command I have?
Was This Post Helpful? 0
  • +
  • -

#11 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: yet another timer question

Posted 23 December 2008 - 09:16 PM

If by resetting the timer you mean setting the Time value back to 0.3 and enabling the Timer control, add the following code to the mnuMouseReset_Click event handler:

   Time = 0.3
   Timer1.Enabled = True


Was This Post Helpful? 0
  • +
  • -

#12 Darkangler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 09-September 08

Re: yet another timer question

Posted 23 December 2008 - 09:19 PM

I don't want the timer to start immediately, I'd like it to be set at 00.30 until the user accesses the arrow keys to move the player figure through the maze. Likewise when the timer reset, I'd like it to stick at 00.30.
Was This Post Helpful? 0
  • +
  • -

#13 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: yet another timer question

Posted 23 December 2008 - 09:21 PM

Then just use the Time = 0.3 part of the code in the resetting button and activate the timer in another event handler (you specified it).
Was This Post Helpful? 0
  • +
  • -

#14 Darkangler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 09-September 08

Re: yet another timer question

Posted 23 December 2008 - 09:22 PM

View PostCore, on 23 Dec, 2008 - 08:21 PM, said:

Then just use the Time = 0.3 part of the code in the resetting button and activate the timer in another event handler (you specified it).


	Private Sub mnuMouseReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuMouseReset.Click
		picPlayer.Top = 36
		picPlayer.Left = 26

		mnuMouseSpeedSlow.Checked = False
		mnuMouseSpeedMedium.Checked = False
		mnuMouseSpeedFast.Checked = False

		Time = 0.3
		tmrTimer.Enabled = True

	End Sub


I tried to set it to 0.3 here, but it doesn't continue as such.
Was This Post Helpful? 0
  • +
  • -

#15 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: yet another timer question

Posted 23 December 2008 - 09:27 PM

Please be more specific with the issue. What doesn't work?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2