在窗体上画1个Label(标签) 2个CommandButton(按钮) 1个 Timer(记时器)
Private Sub Command1_Click()
Timer1.Enabled = True
End Sub
Private Sub Form_Load()
Label1.Caption = "10"
Command2.Enabled = False
Command1.Enabled = True
Timer1.Enabled = False
Timer1.Interval = 1000
End Sub
Private Sub Timer1_Timer()
Label1.Caption = Val(Label1.Caption) - 1
If Label1.Caption = 0 Then
Command2.Enabled = True
Timer1.Enabled = False
End If
End Sub
delphi的代码,其实和vb大同小异
设置timer控件的interval属性为1000
设一个全局变量 timecount 为整型
按钮1点击响应事件:
timecount:=60;
timer.enabled:=true;
在timer1timer函数中写如下代码:
timecount:=timecount-1;
if timecount=0 then
按钮2.enabled:=true;
Dim nIndex As Long
Private Sub Command1_Click()
nIndex = 0
Timer1.Interval = 1000
Timer1.Enabled = True
End Sub
Private Sub Form_Load()
Command1.Enabled = True
Command2.Enabled = False
Timer1.Enabled = False
End Sub
Private Sub Timer1_Timer()
nIndex = nIndex + 1
Label1.Caption = nIndex
If nIndex = 60 Then
Timer1.Enabled = False
Command2.Enabled = True
End If
End Sub
首先设置按钮二Enabled属性为false然后在按钮1中加click事件开始执行timer在timer中结束时加按钮二Enabled属性来true然后退出timer