PDA

Click to See Complete Forum and Search --> : Timer


Spie
Oct 1st, 2000, 03:32 PM
How do I make a timer?
I want it to count down from 500 until it gets to 0.
I have this code in my formload:
----------------
Do
Label3.caption = label3 - 1
loop
----------------
VB gets pissed off and crashes though... Am I doing something wrong?

MasterGoon
Oct 1st, 2000, 10:48 PM
The best way to do it is to use the timer object, though most people hate it, the code would be:


Dim intcount As Integer

Option Explicit

Private Sub Form_Load()
Timer1.Interval = 1000
Timer1.Enabled = True
intcount = 501
End Sub

Private Sub Timer1_Timer()
intcount = intcount - 1
If count <> 1 Then
Label1.Caption = intcount
Else
Timer1.Enabled = False
End If
End Sub


Hope this helps :)

Spie
Oct 2nd, 2000, 02:09 PM
Thanks :)

Spie
Oct 2nd, 2000, 02:14 PM
Um... How do I make it stop at 0 and not go to the - numbers?

Oct 2nd, 2000, 03:09 PM
You can also do it without a Timer.

Sub CountDown(StartValue As Integer, lbl As Label)

lbl = StartValue

Do Until lbl = 0
Start = Timer

Do While Timer < Start + 1
DoEvents
Loop

lbl = lbl - 1
Loop

End Sub

Private Sub Command1_Click()
CountDown 10, Label1
End Sub

MasterGoon
Oct 2nd, 2000, 06:21 PM
Use and if statement to tell it the program it must be more than 0...


Dim intcount As Integer

Option Explicit

Private Sub Form_Load()
Timer1.Interval = 1000
Timer1.Enabled = True
intcount = 501
End Sub

Private Sub Timer1_Timer()
intcount = intcount - 1
If count > 0 Then
Label1.Caption = intcount
Else
Timer1.Enabled = False
End If
End Sub