VB中如何timer 控件进行倒计时

用于考试系统

1、程序加载时操作: Private Sub Form_Load() '窗体加载时自动进行
Timer1.Interval = 1000 ‘设置计时周期为1秒注意默认计时单位为毫秒,即1/1000秒

2、定义一个时间变量。可以某控件的Caption属性代替,如Label12.Caption
3、拖放timer控件到程序界面上
4、设置倒计时:双击时钟控件,输入计时规则,如Label12.Caption = Label12.Caption + 1
5、设置当时间值达到某一条件的时候应采取的方法(即动作)。可以用if语句。如if Label12.Caption =60,then Unload Form1,注意块if语句与行if语句的区别

以下是例子:
Private Sub Form_Load() '窗体加载时自动进行以下操作
Timer1.Interval = 1000 '计时频率设为1秒
end sub
Private Sub Timer1_Timer()
Label12.Caption = Label12.Caption - 1 '以秒计时
End Sub
Private Sub CommandSure_Click()
if Label12.Caption =
s = MsgBox("确认提交,取消重填!", 1, "提示信息") '弹出对话框
If s = 1 Then
f = MsgBox("提交信息成功,请点击确认退出系统", 0, "")
Unload Form1 '窗体关闭
Else
Load Form1 '重新初始化
Form1.Enabled = True '允许重填
Timer1.Enabled = True '继续计时
CommandSure.Enabled = True '确定按钮可用
end if
end sub
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-06-19
Label1.Caption = DateDiff("h", Now, "2010-08-01 12:00")
h 表示返回的为小时
就此你可以整理一下
第2个回答  2010-06-19
dim i as long
Private Sub Timer1_Timer()
i=i-1
if i>=0 then timer1.enabled=false:exit sub
me.caption="还剩下:" & i & "秒"
End Sub
Private Sub Form_Load()
i=50
timer1.interval=1000
end sub