vb倒计时代码

2024-12-25 16:53:52
推荐回答(2个)
回答1:

Dim s As Long

Private Sub cmdSet_Click()
Timer1.Enabled = False
s = InputBox("请输入倒计时秒数:", "设置倒计时秒数", 5)
Label1 = "倒计时: " & s & "秒"
cmdStop.Tag = s
cmdStart.Tag = 0
cmdStart.Caption = "启动时间"
cmdStart.Enabled = s > 0
cmdStop.Enabled = s > 0

End Sub

Private Sub cmdStart_Click()
If cmdStart.Tag = 0 Then
Timer1.Enabled = True
cmdStart.Caption = "暂停时间"
cmdStart.Tag = 1
Else
Timer1.Enabled = False
cmdStart.Caption = "启动时间"
cmdStart.Tag = 0
End If
End Sub

Private Sub cmdStop_Click()
cmdStart.Tag = 0
cmdStart.Caption = "启动时间"
Timer1.Enabled = False
s = cmdStop.Tag

Label1 = "倒计时: " & s & "秒"

End Sub

Private Sub Timer1_Timer()

s = s - 1
Label1 = "倒计时: " & s & "秒"
If s <= 0 Then
Timer1.Enabled = False
cmdStart.Caption = "启动时间"
cmdStart.TabIndex = 0

End If

cmdStart.Enabled = s > 0
cmdStop.Enabled = s > 0

End Sub

回答2:

Dim i As Long

Private Sub Command1_Click()
Select Case Command1.Caption
Case "启动时间"
Command1.Caption = "暂停时间"
Timer1.Enabled = True
Case "暂停时间"
Command1.Caption = "启动时间"
Timer1.Enabled = False
Case Else

End Select

End Sub

Private Sub Form_Load()
Command1.Caption = "启动时间"
Timer1.Enabled = False
Timer1.Interval = 1000
i = CLng(Val(Text1.Text))
End Sub

Private Sub Timer1_Timer()
i = i - 1
Label1.Caption = "倒计时: " & i
If i = 0 Then
Timer1.Enabled = False
Command1.Caption = "启动时间"
End If
End Sub