VB 计时器

来源:百度知道 编辑:UC知道 时间:2024/06/27 13:59:47
编写程序,用计时器按秒计时。在窗体上画一个计时器控件和一个标签,程序行运后,在标签内显示经过的秒数,并响铃。

Private Sub Form_Load()
Timer1.Enabled = True
Timer1.Interval = 5000 '以毫秒为单位,1000=1秒
Label1.Caption= "5" '倒计时5秒显示
End Sub
Private Sub Timer1_Timer()
Static s
s = s + 1
t = 5 - s
Label2.Caption = t
If t = 0 Then
响铃语句。
Beep
End If
End Sub

什么问题?我没测试。就是这么写的。

VB2008和VB2005中:

Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Timer1.Enabled = True
Me.Timer1.Interval = 1000 '以毫秒为单位,1000=1秒
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Me.Label2.Text = s
Static s As Integer = 5
s -= 1
If s = 0 Then Beep()
End Sub

虽然思路上可以实现,但是实际运用中可能出现程序假死现象,就是过好一会才出现一个数字。这是VB的特性决定的。没有办法。