恭请VB高手~利用计时器Timer建立数字秒表!
利用计时器Timer建立数字秒表(如图)。在窗体上画一个标签,用来显示秒表的计时值;画二个按钮,其中一个用来控制秒表的开启/暂停,另一个用来复位(清零)。要求秒表计时精度...
利用计时器Timer建立数字秒表(如图)。在窗体上画一个标签,用来显示秒表的计时值;画二个按钮,其中一个用来控制秒表的开启/暂停,另一个用来复位(清零)。要求秒表计时精度为十分之一秒或百分之一秒。编写适当的事件过程,使程序运行后,按开启/暂停按钮即可开始计时,再按开启/暂停按钮即可暂停;按复位(清零)按钮可清除原有的计时。(提示:通过Tick事件利用变量进行计时)
嗯~先参考下这些, 现在要的是每次增加0.01S ,而且题目说明得很清楚。 麻烦用command控件的童鞋不要复制粘贴。 2楼那位高手只会打字,good job~ 展开
嗯~先参考下这些, 现在要的是每次增加0.01S ,而且题目说明得很清楚。 麻烦用command控件的童鞋不要复制粘贴。 2楼那位高手只会打字,good job~ 展开
4个回答
展开全部
Dim mstime As Single
Private Sub Command1_Click()
If Timer1.Enabled = False Then
Timer1.Enabled = True
ElseIf Timer1.Enabled = True Then
Timer1.Enabled = False
End If
End Sub
Private Sub Command2_Click()
mstime = 0
Timer1.Interval = 10 '0.01秒触发1次
Timer1.Enabled = False
mstime = mstime + 0.01 '去掉本句复位归零
Label1.Caption = Format(mstime, "###0.00")
End Sub
Private Sub Form_Load()
Command1.Caption = "暂停/继续/开始"
Command2.Caption = "复位才能开始" '复位才能开始
End Sub
Private Sub Timer1_Timer()
mstime = mstime + 0.01
Label1.Caption = Format(mstime, "###0.00")
End Sub
Private Sub Command1_Click()
If Timer1.Enabled = False Then
Timer1.Enabled = True
ElseIf Timer1.Enabled = True Then
Timer1.Enabled = False
End If
End Sub
Private Sub Command2_Click()
mstime = 0
Timer1.Interval = 10 '0.01秒触发1次
Timer1.Enabled = False
mstime = mstime + 0.01 '去掉本句复位归零
Label1.Caption = Format(mstime, "###0.00")
End Sub
Private Sub Form_Load()
Command1.Caption = "暂停/继续/开始"
Command2.Caption = "复位才能开始" '复位才能开始
End Sub
Private Sub Timer1_Timer()
mstime = mstime + 0.01
Label1.Caption = Format(mstime, "###0.00")
End Sub
展开全部
使用TIMER定时器是不精确的。跑一会就会发现时间有偏差的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Dim mstime As Single
Private Sub Command1_Click()
If Timer1.Enabled Then
Timer1.Enabled = False
Else
mstime = 0
Timer1.Enabled = True
End If
End Sub
Private Sub Command2_Click()
mstime = 0
End Sub
Private Sub Form_Load()
Timer1.Interval = 10 '0.1秒触发1次
Timer1.Enabled = False
mstime = 0
End Sub
Private Sub Timer1_Timer()
mstime = mstime + 0.01
Label1.Caption = Format(mstime, "###0.00")
End Sub
Private Sub Command1_Click()
If Timer1.Enabled Then
Timer1.Enabled = False
Else
mstime = 0
Timer1.Enabled = True
End If
End Sub
Private Sub Command2_Click()
mstime = 0
End Sub
Private Sub Form_Load()
Timer1.Interval = 10 '0.1秒触发1次
Timer1.Enabled = False
mstime = 0
End Sub
Private Sub Timer1_Timer()
mstime = mstime + 0.01
Label1.Caption = Format(mstime, "###0.00")
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我看你给的是一个vb.net的界面 是不是需要.net的代码啊?我给一个2005的,虽然没有用timer作为计时工具但是要给你一个更好的方法。它能精确到1毫秒。
全部代码:
Public Class Form1
Dim a As Stopwatch, m As TimeSpan
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label1.Text = "00:00:00:000"
Button1.Text = "开启/暂停"
Button2.Text = "复位(清零)"
Timer1.Enabled = True
Timer1.Interval = 1
a = New Stopwatch()
a.Reset()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If a.IsRunning = False Then
a.Start()
ElseIf a.IsRunning = False Then
a.Stop()
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
a.Stop()
a.Reset()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
m = a.Elapsed
Label1.Text = Format(m.Hours, "00") & ":" & Format(m.Minutes, "00") & ":" & Format(m.Seconds, "00") & ":" & Format(m.Milliseconds, "000")
End Sub
End Class
楼主 我看这个问题好像很难啊 如果光用计时器的话,可是要花费一些脑筋啊。。。
如果设定10mm触发一次 那会有巨大的误差。真可悲。
我这还有一个思路。。
我家电脑不是老爷机 但是就算timer的interval=1一秒钟也触发不了100次啊!。
所以我想。10次校准取平均
窗体上一个label一个button,但是还是不可避免的用到了stopwatch。
Public Class Form1
Dim tm As Double, isj As Integer, bz As Double, a As New Stopwatch
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If isj > 0 Then
a.Stop()
bz = (bz * (10 - isj) + a.ElapsedTicks / Stopwatch.Frequency) / (11 - isj)
isj = isj - 1
a.Reset()
End If
tm = tm + bz * 10
Label1.Text = Format(tm, "0.00")
End Sub
Private Sub jiaozhun()
Timer1.Interval = 1
Timer1.Enabled = True
a.Start()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call jiaozhun()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
isj = 10
End Sub
End Class
全部代码:
Public Class Form1
Dim a As Stopwatch, m As TimeSpan
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label1.Text = "00:00:00:000"
Button1.Text = "开启/暂停"
Button2.Text = "复位(清零)"
Timer1.Enabled = True
Timer1.Interval = 1
a = New Stopwatch()
a.Reset()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If a.IsRunning = False Then
a.Start()
ElseIf a.IsRunning = False Then
a.Stop()
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
a.Stop()
a.Reset()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
m = a.Elapsed
Label1.Text = Format(m.Hours, "00") & ":" & Format(m.Minutes, "00") & ":" & Format(m.Seconds, "00") & ":" & Format(m.Milliseconds, "000")
End Sub
End Class
楼主 我看这个问题好像很难啊 如果光用计时器的话,可是要花费一些脑筋啊。。。
如果设定10mm触发一次 那会有巨大的误差。真可悲。
我这还有一个思路。。
我家电脑不是老爷机 但是就算timer的interval=1一秒钟也触发不了100次啊!。
所以我想。10次校准取平均
窗体上一个label一个button,但是还是不可避免的用到了stopwatch。
Public Class Form1
Dim tm As Double, isj As Integer, bz As Double, a As New Stopwatch
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If isj > 0 Then
a.Stop()
bz = (bz * (10 - isj) + a.ElapsedTicks / Stopwatch.Frequency) / (11 - isj)
isj = isj - 1
a.Reset()
End If
tm = tm + bz * 10
Label1.Text = Format(tm, "0.00")
End Sub
Private Sub jiaozhun()
Timer1.Interval = 1
Timer1.Enabled = True
a.Start()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call jiaozhun()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
isj = 10
End Sub
End Class
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询