We are going to use 5 components, also called objects.
- a label as a title (lbltitle)
- a text box (txtuser) where the user enters a the time he wants to countdown in seconds
- another label (lblremain) where the remaining time is displayed
- a timer (tmr) which is the "heart" of the application
- a comand button (cmdstart) which starts the countdown
The 1st label (lbltitle) just tells the user to enter the user the time he wants to countdown. Put as a caption in its properties something like "Enter time to countdown.
Leave the text box (txtuser) blank and do the same to the second label (lblremain).
Use the following code for the command button (cmdstart)
CODE
Private Sub cmdstart_Click()
If Not IsNumeric(txtuser.Text) Then
MsgBox "Enter a value."
Else
tmr.Enabled = True
tmr.Interval = 1000
lblremain.Caption = txtuser.Text
End If
End Sub
I use an if statement which says:
If the text in the text box (txtuser) is not a number then a message box shall appear telling to enter a value.
If the previous statement is not true then it will enabel the timer (tmr) and it will change the Invertal of the timer (tmr) to 1000 (1 second) and will also change the caption of the remainig time to the same value as the user has entered in the text box (txtuser). Don't forget to end the if command.
Use the following code for the timer component (tmr):
CODE
Private Sub tmr_Timer()
If lblremain.Caption = 0 Then
tmr.Enabled = False
MsgBox Text1.Text & " sec are up!", vbInformation
Else
lblremain.Caption = lblremain.Caption - 1
End If
End Sub
This if statement says: If the caption (the characters) in the label (lblremain) eqals to zero (0) then the timer stops. Aftre that a meggasge box appears and stays that the values the user had entered in the text box (txtuser) is over. If the previous statement is false then it will remove one from the label. Because the timer is set to repeat its task every second it will do that until the value reaches zero.