注册 登录
编程论坛 VB6论坛

计时器怎么写啊?

yuma 发布于 2012-04-29 15:37, 610 次点击
下面是我写的,
窗体上有:command1  command2   timer1   label1
以下是我自己写的代码:
程序代码:


Private Sub Form_Load()
Command1.Caption = "开始"
Command2.Caption = "停止"
Dim h As String
Dim m As String
Dim s As String
h = 0
m = 0
s = 0
Timer1.Enabled = False
Timer1.Interval = 1000
Label1.Caption = "00:00:00"
End Sub


Private Sub Command1_Click()
Command1.Enabled = False
Timer1.Enabled = True

End Sub
Private Sub Command2_Click()
Timer1.Enabled = False
Label1.Caption = "00:00:00"
End Sub

Private Sub Timer1_Timer()
s = s + 1
If s = 60 Then
m = m + 1
s = 0
ElseIf m = 60 Then
h = h + 1
m = 0
End If
Label1.Caption = Format(h, "00") & ":" & Format(m, "00") & ":" & Format(s, "00")


End Sub

有错误。


[ 本帖最后由 yuma 于 2012-4-29 16:21 编辑 ]
6 回复
#2
wube2012-05-01 00:23
程序代码:

Private Sub Form_Load()
Command1.Caption = "开始"
Command2.Caption = "停止"
Dim h As String
Dim m As String
Dim s As String
h = 0
m = 0
s = 0
Timer1.Enabled = False
Timer1.Interval = 1000
Label1.Caption = "00:00:00"
End Sub


Private Sub Command1_Click()
Label1.Caption = "00:00:00"
Command1.Enabled = False
Timer1.Enabled = True
End Sub

Private Sub Command2_Click()
Timer1.Enabled = False
Label1.Caption = Format(h, "00") & ":" & Format(m, "00") & ":" & Format(s, "00")
End Sub

Private Sub Timer1_Timer()
if Timer.Enabled =True then
s = s + 1
If s = 60 Then
m = m + 1
s = 0
ElseIf m = 60 Then
h = h + 1
m = 0
End If
End If
End Sub


试试~只在脑袋中运行的过~
#3
wube2012-05-02 09:21
程序代码:

Dim h As intxxx,m As intxxx,s As intxxx

Private Sub Form_Load()
    Command1.Caption = "开始"
    Command2.Caption = "停止"
    Timer1.Enabled = False
    Timer1.Interval = 1000
    Label1.Caption = "00:00:00"
End Sub

Private Sub Command1_Click()
    h = 0:m = 0:s = 0
    Label1.Caption = "00:00:00"
    Command1.Enabled = False
    Timer1.Enabled = True
End Sub

Private Sub Command2_Click()
    Timer1.Enabled = False
    Label1.Caption = Format(h, "00") & ":" & Format(m, "00") & ":" & Format(s, "00")
    Command1.Enabled = True
End Sub

Private Sub Timer1_Timer()
    if Timer.Enabled =True then
        s = s + 1
        If s = 60 Then
            m = m + 1
            s = 0
        ElseIf m = 60 Then
            h = h + 1
            m = 0
        End If
    End If
End Sub
#4
allentj2012-05-02 15:32
程序代码:
Dim s As Integer
Dim m As Integer
Dim h As Integer
Dim t As Integer

Private Sub Command1_Click()
Command1.Enabled = False
Command2.Enabled = True
Timer1.Enabled = True
h = 0
m = 0
s = 0
t = 0
End Sub
Private Sub Command2_Click()
Command1.Enabled = True
Command2.Enabled = False

Timer1.Enabled = False

Label1.Caption = "00:00:00"
End Sub

Private Sub Form_Load()
s = 0
m = 0
h = 0
Timer1.Interval = 1000
Timer1.Enabled = False
End Sub

Private Sub Timer1_Timer()
      s = s + 1
      If s = 60 Then
      m = m + 1
      s = 0
      If m = 60 Then
      h = h + 1
      m = 0
      If h = 24 Then
      t = t + 1
      End If
      End If
      End If
      Label1.Caption = t & "" & Format(h, "00") & ":" & Format(m, "00") & ":" & Format(s, "00")
      
End Sub

只有本站会员才能查看附件,请 登录

注意定义数据类型!

[ 本帖最后由 allentj 于 2012-5-2 15:33 编辑 ]
#5
wube2012-05-02 16:25
VB6的好处是一旦定义数据类型,它就帮你初始化了
所以Form_Load()里就能省略变量初始化的动作.

基本上Form_Load()只跑一次,所以
Command1.Caption = "开始"
Command2.Caption = "停止"
Timer1.Enabled = False
Timer1.Interval = 1000
Label1.Caption = "00:00:00"
这5行在控件属性内直接设定即可,
一般而言Form_Load()整段根本不用写.

仅限VB6
#6
神之罗纳尔多2012-05-03 21:41
新手来学习
#7
yuma2012-05-04 19:48
回复 4楼 allentj
谢谢,已通过调试。
1