窗体中放置三个Command、三个Label、一个Timer控件,在Form_Load过程中已经说明了控件用途,其中Label1是显示分钟数,Label2是显示秒数,代码如下:
Dim JS As Integer '这里声明了一个全局变量,保存计时数
Private Sub Command1_Click()
Timer1.Enabled = False '停止计时
End Sub
Private Sub Command2_Click()
Timer1.Enabled = True '继续计时
End Sub
Private Sub Command3_Click()
Unload Me '重新计时
Form1.Show
End Sub
Private Sub Form_Load()
Command1.Caption = "暂停" '"暂停"按钮
Command2.Caption = "继续" '"继续"按钮
Command3.Caption = "重新计时" '"重新计时"按钮
Label3.Caption = ":" '显示":" 分隔符
Timer1.Enabled = True '使计时开始
Timer1.Interval = 1000 '计时间隔为1分钟(1000毫秒)
JS = 180 '计时数初始化,180秒即3分钟
End Sub
Private Sub Timer1_Timer()
Dim F As Integer, M As Integer
If JS > 0 Then
JS = JS - 1
If JS <= 30 Then '在小于30秒后,标签底色为红色
Label1.BackColor = &HFF
Label2.BackColor = &HFF
Label3.BackColor = &HFF
End If
F = Int(JS / 60) '计算分钟数
M = JS - F * 60 '计算秒数
Label1.Caption = F '在Label1标签显示分钟数
Label2.Caption = M '在Label2标签显示秒数
Else
Timer1.Enabled = False '如果计时数小于或等于0,不再计时
End If
End Sub