当前位置:编程学习 > VB >>

VB倒计时程序代码

做个2分钟的倒计时 两个按钮 开始和停止
没别的特殊要求 时间显示格式要 2:00 这样的
格式不会弄 请教高手
答案:
代码如下:
'窗体全局变量
Dim Hours As Integer '记录小时
Dim Minutes As Integer '记录分钟
Dim Seconds As Integer '记录秒
Dim Time As Date

Private Sub Mydisplay()
Hours = Val(Text1.Text)
Minutes = Val(Text2.Text)
Seconds = Val(Text3.Text)
Time = TimeSerial(Hours, Minutes, Seconds) '组合字符串
'将字符串转化为时间格式
Label1.Caption = Format$(Time, "hh") & ":" & Format$(Time, "nn") & ":" & Format$(Time, "ss")
End Sub
Private Sub Command1_Click()
Timer1.Enabled = True
Command1.Enabled = False
Command2.Enabled = True
Command3.Enabled = True
End Sub
Private Sub Command2_Click()
If Timer1.Enabled Then
Timer1.Enabled = False
End If
Hours = 0
Minutes = 0
Seconds = 0
Time = 0
Command1.Enabled = True
Command3.Enabled = False
Text1.Text = " "
Text2.Text = " "
Text3.Text = " "
Text1.SetFocus '将焦点放到Text1控件上
End Sub
Private Sub Command3_Click()
Timer1.Enabled = False
Command3.Enabled = False
Command1.Enabled = True
Command2.Enabled = True
End Sub
Private Sub Form_Load()
Timer1.Interval = 1000 '将Timer控件的事件引发间隔设置成1000毫秒
'初始化变量
Hours = 0
Minutes = 0
Seconds = 0
Time = 0
End Sub
Private Sub Text1_Change() 'TextBox控件事件,当控件的Text值改变时引发该事件
Mydisplay
End Sub
Private Sub Text2_Change()
Mydisplay
End Sub
Private Sub Text3_Change()
Mydisplay
End Sub
Private Sub Timer1_Timer() 'Timer控件的事件
Timer1.Enabled = False
If (Format$(Time, "hh") & ":" & Format$(Time, "nn") & ":" & Format$(Time, "ss")) <> "00:00:00" Then 'Counter to continue loop until 0
Time = DateAdd("s", -1, Time)
Label1.Caption = Format$(Time, "hh") & ":" & Format$(Time, "nn") & ":" & Format$(Time, "ss")
Timer1.Enabled = True
Else
'如果未输入时间或时间为零则发出警报
Timer1.Enabled = False
Beep
Beep
Command2.Enabled = True
End If
End Sub

上一个:VB如何连接Access数据库
下一个:VB比较常用的变量

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,