求 VB 10秒倒计时代码

倒计时显示在text里
2024-11-07 13:36:50
推荐回答(4个)
回答1:

一个文本控件text1显示倒计时 一个定时器控件定时,一个按钮控件触发倒计时事件
Dim i As Integer '全局变量

Private Sub Command1_Click()
Text1.Text = 10 '文本初始值10
Timer1.Enabled = True '开定时器
Timer1.Interval = 1000 'MS单位
i = 10 ' i赋初始值
End Sub

Private Sub Form_Load()
Text1.Text = "" '文本初始值空值
End Sub
Private Sub Timer1_Timer()
i = i - 1 '每到1s响应定时器,响应次数+1
Text1.Text = i
If i = 0 Then
Timer1.Enabled = False '关定时器
End If
End Sub

回答2:

Dim test As Long
Private Sub Form_Load()
Timer1.Interval = 1000
test = 10
Text1.Text = ""
Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
Text1.Text = test
test = test - 1
If Text = 0 Then
Timer1.Enabled = False
'到时间处理的事件
End If
End Sub

回答3:

加入一个TIMER,设置INTERVAL为1000

OPTION EXPLICT
DIM LNGSECOND AS LONG

PRIVATE SUB FORM_LOAD
LNGSECOND = 10
TEXT1.TEXT = "开始倒数"
END SUB

PRIVATE SUB TIMER_TIMER
IF LNGSECOND = 0 THEN
TIMER.ENABLED = FALSE
EXIT SUB
ELSE
TEXT1.TEXT = LNGSECOND
LNGSECOND = LNGSECOND - 1
ENDIF
END SUB

回答4:

Option Explicit
Private Sub Form_Load()
Timer1.Interval = 1000
End Sub
Private Sub Timer1_Timer()
Static m
m = m + 1
Text1.Text = "剩余" & 11 - m & "秒"
If m = 11 Then MsgBox "时间到!": Timer1.Enabled = False
End Sub