火线出击一共多少集:关于VB问题。

来源:百度文库 编辑:杭州交通信息网 时间:2024/04/24 09:14:09
我是初学者哈,刚刚接触时间控件,现在有一个练习是要编一个倒计时器,Timer的属性Interval为1000,Enabled为false,有按钮激发为True,问题就是Timer的代码我总是编不对,无法实现倒计时,请问那位前辈为我指点迷津?

Private Sub Command1_Click()
Timer1 = True
End Sub

Private Sub Timer1_Timer()
Text1 = Val(Text1) - 1 '秒数减1
If Val(Text1) = 0 Then MsgBox "时间到":beep
End Sub
text1可自己设数值(倒计时秒数)

如下代码可实现与电脑时钟同步精度的倒记时功能,修改sum_zj1的设置值可改变倒记时值.注意Timer1的属性Interval设置在500:
Option Explicit
Dim sum_zj As Integer
Dim sum_zj1 As Integer

Private Sub Form_Load()
sum_zj = 0: sum_zj1 = 20 '倒记时60秒
Text1 = sum_zj: Text2 = sum_zj1
End Sub
Private Sub Timer1_Timer()
If Label1.Caption <> CStr(Time$) Then
Label1.Caption = Time$
sum_zj = sum_zj + 1
Text1 = sum_zj
'3秒校准一次
If sum_zj >= 3 And sum_zj1 > 0 Then
sum_zj1 = sum_zj1 - 1
sum_zj = sum_zj - 3
Text2 = sum_zj1
ElseIf sum_zj1 <= 0 Then
Form1.BackColor = vbRed
Timer1.Enabled = False
End If
End If
End Sub

没有那么麻烦吧。。。。

Dim x As Long '定义x为所需要大小 的长整数

Private Sub command1_click()
x = InputBox("请输入所需时间") '使X得到相应 的时间附值
Timer1.Enabled = True '开启timer1
End Sub

Private Sub timer1_timer()
If x > 0 Then '判定 X > 0
x = x - 1
Else
Beep '报警
m = MsgBox("时间到") '弹出对话框
Timer1.Enabled = False '结束计时器
End If

End Sub

Public Tim_Num AS integer'定义一个全程变量,为倒记时的时间总数(秒)

'在程序开始或者需要的地方为倒记时的时间总数Tim_Num附值:

Tim_Num =60

Private Sub Timer1_Timer()

Tim_Num=Tim_Num-1

if Tim_Num=0 then
'时间到了后执行的代码
'……
'……
Timer1.Enabled=false
end if

label1.caption="倒记时:" & Tim_num & "秒"

End Sub

在需要倒计时的地方(如按钮的click事件代码)加入:
tim_num=60
timer1.enable=true
timer的代码正确,能倒计时

如果你要让他自己不停的到计时走下去就要用到线程了