Ⅰ 如何用VB程序做計時器
計時器是在每一定時間間隔就觸發一次事件,這個時間可在計時器的interval屬性中設定,默認值為0,表示計時器不工作。
如果觸發事件後不關閉計時器(設置enabled屬性為false,功能與interval=0時一樣),它就每隔一定時間不斷觸發事件,直到程序結束或 interval屬性值設為0 或 enabled屬性設為0
倒計時5秒結束後自動關閉form2:
**Timer是不準確,但精度要求不高時還是可以的。
1.新建標准EXE工程,在Form1添加一計時器Timer1,一文本框Text1,再加一窗體:form2
2.寫入下面代碼(有兩方案),但以下還是保留
[方案一:最簡單的,看不到倒數過程]
private sub Form_load()
form2.show '運行窗體2
timer1.interval=5*1000 '5000毫秒,即5秒
end sub
Private Sub Timer1_Timer()
timer1.enabled=false '關閉計時器
unload form2 '關閉form2
End Sub
[方案二:看到倒數過程]
private sub Form_load()
form2.show '運行窗體2
text1.text="5"
timer1.interval=1*1000 '1000毫秒,即1秒
end sub
Private Sub Timer1_Timer()
Rem text1的值減1
'{
Dim a as integer
a=val(text1.text) 'text1.text字串轉整數
a=a-1
text1.text=str(a) '整數轉字串放回text1.text
'}
Rem 檢查text1的值
'/*
if a<=0 then
unload form2 '關閉form2
timer1.enabled=false '關閉計時器
'*/
End Sub
Ⅱ 求設計vb計時器的代碼,要求三個文本框內分別是小時、分和秒的走動
畫三個文本框,分別為H、M、S和一個計時器,代碼如下:
Private
Sub
Timer1_Timer()
H.Text
=
Hour(Now)
『顯示時
M.Text
=
Minute(Now)
『顯示分
S.Text
=
Second(Now)
』顯示秒
End
Sub
Ⅲ vb1分鍾計時器
Dim i As Integer
Private Sub Form_Load()
Timer1.Interval = 1000
Label1.Caption = 0
End Sub
Private Sub Timer1_Timer()
i = i + 1
Label1.Caption = i
If i >= 60 Then
i = 0
MsgBox "1分鍾了!"
End If
End Sub
Ⅳ VB計時器代碼
使用timer控制項,把屬性interval設置為1000
然後再timer_timer事件中添加加1方法即可。
如圖
代碼如下:
Public
FTime
As
Integer
Private
Sub
Command1_Click()
Me.Timer1.Enabled
=
True
End
Sub
Private
Sub
Command2_Click()
Me.Timer1.Enabled
=
False
End
Sub
Private
Sub
Form_Load()
Me.Timer1.Enabled
=
False
Me.Timer1.Interval
=
1000
End
Sub
Private
Sub
Timer1_Timer()
FTime
=
FTime
+
1
Me.Label1.Caption
=
"計時(秒):"
&
FTime
End
Sub
Ⅳ 求計時器 vb代碼
很簡單的,在窗體放置一個Timer1控制項,四個Text1、Text2、Text3、Text4控制項,二個command1和command2控制項,其Caption屬性分別是「開始」和「結束」,在代碼窗口添加下面代碼:
Dim X As Integer,PX As String
Private Sub command1_Click()
PX = "YES"
Timer1.Enabled = True
Text2.Text = Text1.Text
End Sub
Private Sub command2_Click()
PX = ""
Timer1.Enabled = False
Text3.Text = Text1.Text
Text4.Text = X
End Sub
Private Sub Form_Load()
Timer1.Enabled = True
Timer1.Interval = 1000
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
End Sub
Private Sub Timer1_Timer()
Text1.Text = Now
IF PX = "YES" THEN
X = X + 1
END IF
End Sub
Timer1.Interval = 1000 是設置計時時間間隔為1秒。
如果計時結束時X的值特別大,則X的數據類型選擇單精度或雙精度,至於「顯示,開始到結束持續的時間」目前顯示的是秒,如果你需要轉換成「年-月-日 時:分:秒」的格式,你自己慢慢琢磨吧。
Ⅵ 用VB編寫一個計時器,怎麼編
private sub cmd1_click()
if cmd1.caption="開始" then
text1=format(now-now,"hh:mm:ss")
timer1.enabled=ture
cmd1.caption="暫停"
else
timer1.enabled=false
cmd1.caption="繼續"
end sub
private cmd2_click()
text1=format(now-now,"hh:mm:ss")
timer1.enabled=ture
end sub
private timer1_timer()
text1=Format(DateAdd("s", 1, cdate(text1)), "hh:mm:ss")
end sub
Ⅶ 求一段VB倒計時器代碼
'建立一個text1,一個label1,一個timer1,兩個command1-2,不用設置任何屬性。
Option Explicit
Dim M As Single, S As Long
Private Sub Form_Load()
form1.Width = 3800
form1.Height = 1800
form1.BackColor = RGB(150, 120, 80)
Timer1.Interval = 1000
Timer1.Enabled = False
Text1.Width = 2400
Text1.Height = 300
Text1.Left = 100
Text1.Top = 400
Text1.ForeColor = &H909090
Text1.Visible = True
Command1.Width = 900
Command1.Height = 400
Command1.Left = 2600
Command1.Top = 150
Command2.Width = 900
Command2.Height = 400
Command2.Left = 2600
Command2.Top = 650
Label1.Width = 2400
Label1.Height = 1000
Label1.Left = 100
Label1.Top = 100
Label1.BackColor = &H0
Label1.ForeColor = &H4410FF
Label1.Alignment = 1
Label1.FontSize = 50
Label1.Caption = ""
Label1.Visible = False
Text1.Text = "在此輸入分鍾,再點擊開始"
Command1.Caption = "開始"
Command2.Caption = "退出"
Command1.Enabled = False
End Sub
Private Sub command1_Click()
If Command1.Caption = "開始" Then
If Text1.Text = "" Then Exit Sub
M = CSng(Val(Text1.Text))
If M <= 0 Then Text1.Text = "": Exit Sub
S = M * 60
If S = 0 Then Text1.Text = "": Exit Sub
Text1.Visible = False
Label1.Caption = Str(S)
Label1.Visible = True
Timer1.Enabled = True
Command1.Caption = "停止"
ElseIf Command1.Caption = "停止" Then
Timer1.Enabled = False
Command1.Caption = "繼續"
Command2.Caption = "重新開始"
ElseIf Command1.Caption = "繼續" Then
Timer1.Enabled = True
Command1.Caption = "停止"
Command2.Caption = "退出"
End If
End Sub
Private Sub command2_Click()
If Command2.Caption = "重新開始" Then
Form_Load
ElseIf Command2.Caption = "退出" Then
End
End If
End Sub
Private Sub Text1_Change()
Command1.Enabled = True
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Text1.Text = "在此輸入分鍾,再點擊開始" Then
Text1.ForeColor = &H0
Text1.Text = ""
End If
End Sub
Private Sub Timer1_Timer()
S = S - 1
Label1.Caption = Trim(Str(S))
If S = 0 Then
MsgBox "時間到!!!"
Beep
Timer1.Enabled = False
Label1.Visible = False
Text1.Visible = True
Command1.Caption = "開始"
End If
End Sub