Countdown Timer

 

Today I came across another VB question under the Yahoo! Answer section. It goes like this "how do i make a timer in vb6 that counts down from 10 - 0 ten times in a row? i can get the timer to countdown, but it won't count stop after 10 times, it goes on forever. i've tried declaring a counter variable and increasing it everytime it hit's 0, but it seems to screw up the program.....so how do i make it count down 10 times in a row?." . Below is the sample program for the above question:

 

Dim i, j, counter As Integer

Public Sub Command1_Click()
Timer1.Enabled = True
End Sub

Private Sub Form_Load()

Label1.Caption = 10

End Sub

Private Sub Timer1_Timer()
j = j + 1
i = 10 - j

Label1.Caption = i

If i = 0 Then
j = -1
End If
counter = counter + 1

If counter = 109 Then
Timer1.Enabled = False
Else
Timer1.Enabled = True


End If

End Sub


 

Explanations

 This is a count down timer that counts from 10 to 0 , repeated 10 times.

In this program, you need to insert a timer and set the property enabled to false at startup.

Label1 is used to display the time. The timer's interval is set to 1000, which is equivalent to 1 second.

j is used as a counter for individual loop while counter is used for the whole duration.

 

 

 

 

 

 

 

 

 [Back to VBToday]