'在窗体上放一个Label控件,一个CommandButton控件,一个Timer控件 Dim counter As Integer Private Sub Command1_Click() If Command1.Caption = "计时开始" Then Command1.Caption = "计时结束" counter = 0 Timer1.Enabled = True Else Command1.Caption = "计时开始" Timer1.Enabled = False End If End SubPrivate Sub Form_Load() Timer1.Interval = 10 Timer1.Enabled = False Command1.Caption = "计时开始" End SubPrivate Sub Timer1_Timer() counter = counter + 1 Label1.Caption = Format(counter, "00:00:00") End Sub
AustinLei(黄瓜杀手)的代码需要改进一下: '在窗体上放一个Label控件,一个CommandButton控件,一个Timer控件Dim dtBegin as date Private Sub Command1_Click() If Command1.Caption = "计时开始" Then Command1.Caption = "计时结束" dtBegin=now() Timer1.Enabled = True Else Command1.Caption = "计时开始" Timer1.Enabled = False End If End SubPrivate Sub Form_Load() Timer1.Interval = 500 Timer1.Enabled = False Command1.Caption = "计时开始" End SubPrivate Sub Timer1_Timer() Label1.Caption = Format(now()-dtBegin, "hh:mm:ss") End Sub
Dim counter As Integer
Private Sub Command1_Click()
If Command1.Caption = "计时开始" Then
Command1.Caption = "计时结束"
counter = 0
Timer1.Enabled = True
Else
Command1.Caption = "计时开始"
Timer1.Enabled = False
End If
End SubPrivate Sub Form_Load()
Timer1.Interval = 10
Timer1.Enabled = False
Command1.Caption = "计时开始"
End SubPrivate Sub Timer1_Timer()
counter = counter + 1
Label1.Caption = Format(counter, "00:00:00")
End Sub
'在窗体上放一个Label控件,一个CommandButton控件,一个Timer控件Dim dtBegin as date
Private Sub Command1_Click()
If Command1.Caption = "计时开始" Then
Command1.Caption = "计时结束"
dtBegin=now()
Timer1.Enabled = True
Else
Command1.Caption = "计时开始"
Timer1.Enabled = False
End If
End SubPrivate Sub Form_Load()
Timer1.Interval = 500
Timer1.Enabled = False
Command1.Caption = "计时开始"
End SubPrivate Sub Timer1_Timer()
Label1.Caption = Format(now()-dtBegin, "hh:mm:ss")
End Sub
我的代码基本上还可以,就是比较占用资源,并且计时时间长的话误差比较大,短一点倒没什么问题。