必须要用文本框显示倒计时吗?我做了一个只用了计时器的交通灯,也带倒计时的,图示如下:
窗体上加一个Picture控件,一个Text,一个Timer控件,属性都在Form_Load中设置了,另外在工程保存的文件夹中加入三个图片,分别是green.jpg red.jpg yellow.jpg 当然可以换成你所要换的图片。
Option Explicit
Private i As Integer
Private greenlight As Boolean
Private redlight As Boolean
Private Sub Form_Load()
Timer1.Interval = 1000
greenlight = True
redlight = False
Text1.Text = 60
Picture1.Picture = LoadPicture(App.Path & "\green.jpg")
i = 1
End Sub
Private Sub Timer1_Timer()
Text1.Text = Val(Text1.Text) - 1
If Text1.Text = 0 Then
If i = 1 Then '绿灯时
Call yellow
i = 2
ElseIf i = 2 Then '黄灯时
If greenlight Then '黄灯亮后红灯亮
i = 3
greenlight = False
redlight = True
Call red
Else '黄灯亮后绿灯亮
i = 1
redlight = False
greenlight = True
Call green
End If
ElseIf i = 3 Then '红灯时
Call yellow
i = 2
End If
End If
End Sub
Sub green()
Picture1.Picture = LoadPicture(App.Path & "\green.jpg")
Text1.Text = 60
End Sub
Sub red()
Picture1.Picture = LoadPicture(App.Path & "\red.jpg")
Text1.Text = 60
End Sub
Sub yellow()
Picture1.Picture = LoadPicture(App.Path & "\yellow.jpg")
Text1.Text = 10
End Sub
Dim i As Integer
Private Sub Form_Load()
Text1.Text = 0
i = 0
End Sub
Private Sub Timer2_Timer()
If (i = 0) And (Val(Text1.Text) = 0) Then
picture1.picture=loadpicture(1)
Text1.Text = 10
i = i + 1
End If
If (Val(Text1.Text) >= 1) Then
Text1.Text = Str(Val(Text1.Text) - 1)
End If
If (i = 1) And (Val(Text1.Text) = 0) Then
Text1.Text = 11
picture1.picture=loadpicture(2)
i = i + 1
End If
If (i = 2) And (Val(Text1.Text) = 0) Then
Text1.Text = 12
picture1.picture=loadpicture(3)
i = i + 1
End If
If (i > 2) Then
i = 0
End If
End Sub
楼上说得很对呵呵,实现确实麻烦一些,呵呵,不好意思那,图片的就按照你说的改一下
picture1.picture=loadpicture(图片文件的文件名) ,比如1,2,3
korean_sz 把ture写错了,正确为true
而且实现略麻烦呀,放了3个picture控件。
picture动态加图片为:
picture1.picture=loadpicture(图片文件的文件名)