VB中如何设计一个类似60秒倒计时的程序
答案:6 悬赏:10
解决时间 2021-03-05 13:51
- 提问者网友:活着好累
- 2021-03-04 23:26
VB中如何设计一个类似60秒倒计时的程序
最佳答案
- 二级知识专家网友:撞了怀
- 2021-03-04 23:38
创建一个Timer控件:Timer1
设置Timer1的Interval属性为1000
创建一个显示倒计时的Lable:lblTime
代码:
dim i as long
private sub form_load()
i=60
endsub
private sub timer1_timer()
i=i-1
lblTime.caption="倒计时" & i
if i<=0 then
msgbox "倒计时结束,退出"
end
endif
end sub
设置Timer1的Interval属性为1000
创建一个显示倒计时的Lable:lblTime
代码:
dim i as long
private sub form_load()
i=60
endsub
private sub timer1_timer()
i=i-1
lblTime.caption="倒计时" & i
if i<=0 then
msgbox "倒计时结束,退出"
end
endif
end sub
全部回答
- 1楼网友:琴狂剑也妄
- 2021-03-05 02:24
在窗体中加个timer,然后用如下代码
Dim i As Integer
Private Sub Form_Load()
i = 60
Timer1.Interval = 1000
End Sub
Private Sub Timer1_Timer()
i = i - 1
Form1.Cls
Print i
If i = 0 Then
Timer1.Enabled = False
MsgBox ("Time Over!")
End If
End Sub
Dim i As Integer
Private Sub Form_Load()
i = 60
Timer1.Interval = 1000
End Sub
Private Sub Timer1_Timer()
i = i - 1
Form1.Cls
Print i
If i = 0 Then
Timer1.Enabled = False
MsgBox ("Time Over!")
End If
End Sub
- 2楼网友:舊物识亽
- 2021-03-05 02:12
用Timer控件。
- 3楼网友:長槍戰八方
- 2021-03-05 01:07
Dim flag As Boolean '定义布尔型变量,用于判断倒计时间是否已到.若倒计时到,则停止游戏
Private Sub Form_Load()
Label1.Caption = 60
Timer1.Interval = 1000
End Sub
Private Sub Timer1_Timer()
Label1.Caption = Label1.Caption - 1
If Label1.Caption = 0 Then
Label1.Caption = "游戏结束"
flag = True '此时倒计时时间到,设置flag为true(flag系统默认初值为false),用以作为结束程序标志
Timer1.Enabled = False
End If
End Sub
上面就是倒计时的程序
你可以执行游戏的代码处进行如下判断
....
if flag =true then
exit sub
else
....你的游戏代码
Private Sub Form_Load()
Label1.Caption = 60
Timer1.Interval = 1000
End Sub
Private Sub Timer1_Timer()
Label1.Caption = Label1.Caption - 1
If Label1.Caption = 0 Then
Label1.Caption = "游戏结束"
flag = True '此时倒计时时间到,设置flag为true(flag系统默认初值为false),用以作为结束程序标志
Timer1.Enabled = False
End If
End Sub
上面就是倒计时的程序
你可以执行游戏的代码处进行如下判断
....
if flag =true then
exit sub
else
....你的游戏代码
- 4楼网友:纵马山川剑自提
- 2021-03-05 00:36
用Timer控件。很容易实现。
- 5楼网友:鸽屿
- 2021-03-05 00:05
添加timer控件,添加Label控件,label的caption属性为60,将interval属性设为1000(每一秒执行一次)然后在timer的time中添加
Dim i As Integer '定义循环变量
Private Sub Timer1_Timer()
i=60 '定义变量值循环,60次
i = i - 1 '每次循环i的值减去一
label.caption="还有"& i & "秒" '输出i的值,即实现倒计时显示
If i <= 0 Then '判断i的值是否为零,即循环是否到达60次
代码块 '是的话执行代码块中的内容,就是你想要实现的功能,如:"msgbox “游戏结束”"
Timer1.Enabled = False '最后关闭timer,不再循环
End If '退出if语句
End Sub
Dim i As Integer '定义循环变量
Private Sub Timer1_Timer()
i=60 '定义变量值循环,60次
i = i - 1 '每次循环i的值减去一
label.caption="还有"& i & "秒" '输出i的值,即实现倒计时显示
If i <= 0 Then '判断i的值是否为零,即循环是否到达60次
代码块 '是的话执行代码块中的内容,就是你想要实现的功能,如:"msgbox “游戏结束”"
Timer1.Enabled = False '最后关闭timer,不再循环
End If '退出if语句
End Sub
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯