Using a Timer to CountDown in VB.net

  • Thread starter Thread starter WorldIsEnding
  • Start date Start date
W

WorldIsEnding

How could i use the Timer component to count down from a certain
number that i specify thats been entered into a variable?

For example;
Lets say i want to book a table at my local Snooker Club. When i go
in, im given a set of balls and a table to play on - in turn, the
person operating the console behind the bar switches on the tables
lights and sets it so it will automatically turn off after, lets say,
3 hours.

Is there any way i could do this?
If this doesnt make much sense, lemme know and ill try to explain it a
little clearer >.<

Cheers,

3x0DuS
 
Add the timer object, set Interval to 1000ms (1 second). Add a textbox and a
button.

Then u can use something like this:

Dim myTime As Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
myTime = TextBox1.Text
Timer1.Enabled = True
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
TextBox1.Text = myTime - 1
myTime = TextBox1.Text
End Sub

--

Tiago Salgado

http://weblogs.pontonetpt.com/tiagosalgado
http://www.foruns.org
http://www.portugal-a-programar.org
http://www.revista-programar.info
 
How could i use the Timer component to count down from a certain
number that i specify thats been entered into a variable?

For example;
Lets say i want to book a table at my local Snooker Club. When i go
in, im given a set of balls and a table to play on - in turn, the
person operating the console behind the bar switches on the tables
lights and sets it so it will automatically turn off after, lets say,
3 hours.

Is there any way i could do this?
If this doesnt make much sense, lemme know and ill try to explain it a
little clearer >.<

Cheers,

3x0DuS

This seems to simple, so I must be missing something but here it
goes...

On the timer.tick event decrement the variable and when it equals zero
turn off the lights?

Thanks,

Seth Rowe
 
Yeah, pretty much. I would check to see if the variable <= 0 instead
of just zero as a butt covering.

The example code above gives you the right idea, but the convesions
between strings and integers may generate warnings in the compiler.
The code should be casting integers to strings and strings to
integers.

Also, if you want to stop the timer event from firing, you'll want to
set the enabled property to false.

If MyTime <= 0 Then
Timer1.Enabled = False
' Raise some event here or fire a subroutine
EndIf
 
Back
Top