'Timer属性初始值:
'Enable = False
'Interval = 1000
'Command1为开始/停止按钮,属性初始值:
'Caption = "开始"
'Label1为标签,属性初始值:
'Caption = "0"
Dim LostSecond As Double
Private Sub Command1_Click()
With Command1
If Caption = "开始" Then Caption = "停止" Else Caption = "开始"
End With
LostSecond = CDbl(Now)
Timer1Enabled = Not Timer1Enabled
If Timer1Enabled = False Then Label1 = "0"
End Sub
Private Sub Timer1_Timer()
Label1 = Format(CDbl(Now) - LostSecond, "h:mm:ss")
End Sub
Dim s As Integer, m As Integer, h As Integer, n As Integer
Private Sub Form_Load()
Command1Caption = "Start"
Label1Caption = "00:00:00"
Label1Alignment = 2
End Sub
Private Sub Command1_Click()
n = n + 1
If n = 1 Then
Timer1Interval = 1000
Command1Caption = "Close"
End If
If n = 2 Then
Timer1Interval = 0
Command1Caption = "Reset"
End If
If n = 3 Then
Label1Caption = "00:00:00"
Command1Caption = "Start"
n = 0
End If
End Sub
Private Sub Timer1_Timer()
s = s + 1
If s = 60 Then
m = m + 1
s = 0
End If
If m = 60 Then
h = h + 1
m = 0
End If
Label1Caption = Right("0" & h, 2) & ":" & Right("0" & m, 2) & ":" & Right("0" & s, 2)
End Sub
Timer 控件
通过引发 Timer 事件,Timer 控件可以有规律地隔一段时间执行一次代码。
Timer 事件
在一个 Timer 控件的预定的时间间隔过去之后发生。该间隔的频率储存于该控件的 Interval 属性中,它以千分之一秒为单位指定时间的长度。语法:Private Sub object_Timer([index As Integer])
Timer 事件语法包括下列部分:
部分 描述
object 一个对象表达式,其值是“应用于”列表中的一个对象。
index 一个整数,用来唯一地标识一个在控件数组中的控件。
说明
使用 Timer 事件时,可用此事件在每次 Timer 控件时间间隔过去之后通知 Visual Basic 应该做什么:
Interval 属性以千分之一秒为单位指定 Timer 事件之间的间隔。
无论何时,只要 Timer 控件的 Enabled 属性被设置为 True 而且 Interval 属性大于 0,则 Timer 事件以 Interval 属性指定的时间间隔发生。
第一步,打开vb
第二部,添加一个计时器timer,一个文字标签label
第三部,输入代码,代码如下
Private Sub Form_Load()
picClockMove (MeScaleWidth - 8 210) / 2, (MeScaleHeight - 315) / 2, 8 210, 315
Dim i&
For i = 0 To 7
picClockPaintPicture picNumberPicture, i 210, 0, 210, 315, IIf((i = 2) Or (i = 5), 12, 10) 210, 0, 210, 315
Next i
End Sub
Private Sub Timer1_Timer()
Dim cNow$, i&
cNow = Format(Now, "hh:mm:ss")
For i = 0 To 7
If Mid(cNow, i + 1, 1) <> ":" Then
picClockPaintPicture picNumberPicture, i 210, 0, 210, 315, Val(Mid(cNow, i + 1, 1)) 210, 0, 210, 315
End If
Next i
End Sub
望采纳
上面的代码错了,下面是正确代码
Private Sub Form_Load()
Timer1Enabled = True
Timer1Interval = 300
End Sub
Private Sub Timer1_Timer()
Label1 = Now
End Sub
加一个timer控件,interval设为1000,enable设为F,加入以下代码:
Dim a As Long
Private Sub Command1_Click() &&开始
a=0
Timer1Enabled = True
End Sub
Private Sub Command2_Click() &&停止
Timer1Enabled = false
End Sub
Private Sub Timer1_Timer()
Text1Text = Format(Str(a \ 3600) + ":" + Str(a \ 60 - (a \ 3600) 60) + ":" + Str(a - (a \ 60) 60), "hh:mm:ss")
a = a + 1
End Sub
运行通过,请不要怀疑
下面是一个时钟的代码,
在对象窗口需要画一个圆shape1,随便画三条线
接着运行就ok了我试了就用一个timer和 line就行,只是要把line 在属性里改 index=0
Option Explicit
Private Sub Form_Load()
Timer1Interval = 1000
Form1Width = 4000
Form1Height = 4000
Form1Left = ScreenWidth \ 2 - 2000
Form1Top = (ScreenHeight - Height) \ 2
End Sub
Private Sub Form_Resize()
Dim i, Angle
Static flag As Boolean
If flag = False Then
flag = True
'画出表盘12个点和时、分、秒共15个Line
For i = 0 To 14
If i > 0 Then Load Line1(i)'这里容易出错,要在line属性里设置index=0
Line1(i)Visible = True
Line1(i)BorderWidth = 5
Line1(i)BorderColor = RGB(0, 128, 0) '设置Line的粗细和颜色
Next i
End If
Scale (-1, 2)-(1, -2)
For i = 0 To 14
Angle = i 2 Atn(1) / 3
Line1(i)X1 = 09 Cos(Angle)
Line1(i)Y1 = 09 Sin(Angle)
Line1(i)X2 = Cos(Angle)
Line1(i)Y2 = Sin(Angle)
Next i
End Sub
Private Sub timer1_Timer()
Const HH = 0
Const MH = 13
Const SH = 14
Dim Angle
Angle = 05236 (15 - (Hour(Now) + Minute(Now) / 60)) '设置时针
Line1(HH)X1 = 0
Line1(HH)Y1 = 0
Line1(HH)X2 = 03 Cos(Angle)
Line1(HH)Y2 = 03 Sin(Angle)
Angle = 01047 (75 - (Minute(Now) + Second(Now) / 60)) '设置分针
Line1(MH)BorderWidth = 3
Line1(MH)X1 = 0
Line1(MH)Y1 = 0
Line1(MH)X2 = 07 Cos(Angle)
Line1(MH)Y2 = 07 Sin(Angle)
Angle = 01047 (75 - Second(Now)) '设置秒针
Line1(SH)BorderWidth = 1
Line1(SH)X1 = 0
Line1(SH)Y1 = 0
Line1(SH)X2 = 08 Cos(Angle)
Line1(SH)Y2 = 08 Sin(Angle)
Form1Caption = Str(Now()) '窗口显示精确的日期和数字化的时间now=date+time怎样显示星期几weekday?
End Sub
以上就是关于VB程序中,用定时器timer制作一个秒表,当按下开始时,标签从0开始,每隔一秒加1,按下停止,停全部的内容,包括:VB程序中,用定时器timer制作一个秒表,当按下开始时,标签从0开始,每隔一秒加1,按下停止,停、用vb创建一个秒表、vb中秒表的准确讲解等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)