| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 610 人关注过本帖
标题:计时器怎么写啊?
只看楼主 加入收藏
yuma
Rank: 12Rank: 12Rank: 12
来 自:银河系
等 级:贵宾
威 望:37
帖 子:1934
专家分:3012
注 册:2009-12-22
结帖率:89.13%
收藏
 问题点数:0 回复次数:6 
计时器怎么写啊?
下面是我写的,
窗体上有: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 编辑 ]
搜索更多相关主题的帖子: 计时器 
2012-04-29 15:37
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1820
专家分:3681
注 册:2011-3-24
收藏
得分:0 
程序代码:
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


试试~只在脑袋中运行的过~

不要選我當版主
2012-05-01 00:23
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1820
专家分:3681
注 册:2011-3-24
收藏
得分:0 
程序代码:
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

不要選我當版主
2012-05-02 09:21
allentj
Rank: 1
等 级:新手上路
帖 子:3
专家分:1
注 册:2010-12-22
收藏
得分:0 
程序代码:
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

界面.zip (12.24 KB)

注意定义数据类型!

[ 本帖最后由 allentj 于 2012-5-2 15:33 编辑 ]
2012-05-02 15:32
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1820
专家分:3681
注 册:2011-3-24
收藏
得分:0 
VB6的好处是一旦定义数据类型,它就帮你初始化了
所以Form_Load()里就能省略变量初始化的动作.

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

仅限VB6

不要選我當版主
2012-05-02 16:25
神之罗纳尔多
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-5-3
收藏
得分:0 
新手来学习
2012-05-03 21:41
yuma
Rank: 12Rank: 12Rank: 12
来 自:银河系
等 级:贵宾
威 望:37
帖 子:1934
专家分:3012
注 册:2009-12-22
收藏
得分:0 
回复 4楼 allentj
谢谢,已通过调试。

心生万象,万象皆程序!
本人计算机知识网:http://bbs.为防伸手党,本站已停止会员注册。
2012-05-04 19:48
快速回复:计时器怎么写啊?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.031312 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved