How can you make a counter start at a number, count down to 0(or any #) and display this countdown through a label.
Printable View
How can you make a counter start at a number, count down to 0(or any #) and display this countdown through a label.
Code:Private Declare Sub Sleep Lib "kernel32" _
(ByVal dwMilliseconds As Long)
Private Sub Command1_Click()
For i = 1000 To 0 Step -1
Label1.Caption = i
Sleep 1: DoEvents
Next i
End Sub
Why do all that when a timer control can be used?
Anyway, doesnt the sleep function freeze the entire application for the set time?
Yeah, u could simply just put the code insidde the timer. (with the label.caption and counter values)
Why do all what? It ain't much.
I like my way. But if you insist...
And sleep didn't seem to freeze me. Perhaps, the DoEvents came in? :rolleyes:Code:Private Sub Timer1_Timer()
Label1.Caption = Val(Label1.Caption) - 1
End Sub
all i wanted to have was a countdown that could be shown through a label. None of the code worked. I know how to countdown starting at 0. I want to start at five and countdown to 0. Please help!
Second version (it will stop the countdown at exactly zero not one second after (like previous code)Code:'//Add Timer and Label to the Form
Option Explicit
Private Sub Command1_Click()
Timer1.Enabled = True
Timer1.Interval = 1000
End Sub
Private Sub Form_Load()
Label1.Caption = 5
End Sub
Private Sub Timer1_Timer()
If Label1.Caption <> 0 Then
Label1.Caption = Label1.Caption - 1
Else
MsgBox "End of countdown"
Timer1.Enabled = False
End If
End Sub
'//HTH
[Edited by QWERTY on 10-29-2000 at 01:09 AM]Code:'//Add Timer and Label to the Form
Option Explicit
Private Sub Command1_Click()
Timer1.Enabled = True
Timer1.Interval = 1000
End Sub
Private Sub Form_Load()
Label1.Caption = 5
End Sub
Private Sub Timer1_Timer()
If Label1.Caption > 1 Then
Label1.Caption = Label1.Caption - 1
ElseIf Label1.Caption = 1 Then
Timer1.Interval = 1
Label1.Caption = 0
ElseIf Label1.Caption = 0 Then
MsgBox "End of countdown"
Timer1.Enabled = False
End If
End Sub
'//HTH