1樓:
如圖在窗體上新增:
1、四個shape控制元件,作為燈框、紅燈、黃燈、綠燈,分別命名為:lampbox、redlamp、yellowlamp、greenlamp
2、一個命令按鈕,命名為:cmdrun,標題為「啟動」
3、三個文字框控制元件,來設定紅燈、黃燈、綠燈亮的時間(秒),分別命名為:txtredtime、txtyellowtime、txtgreentime
4、四個時鐘控制元件,作為紅燈、黃燈、綠燈和閃爍時間控制,分別命名為:tmrred、tmryellow、tmrgreen、tmrchange
5、其它控制元件和佈局見圖
程式如下:
private red as long
private yellow as long
private green as long
private black as long
private lampno as integer
private sub cmdrun_click()
if cmdrun.caption = "啟動" then
txtredtime.enabled = false
txtyellowtime.enabled = false
txtgreentime.enabled = false
cmdrun.caption = "停止"
tmrred.interval = val(txtredtime.text) * 1000
tmryellow.interval = val(txtyellowtime.text) * 1000
tmrgreen.interval = val(txtgreentime.text) * 1000
lampno = 0
tmrchange.enabled = true
else
tmrred.enabled = false
tmryellow.enabled = false
tmrgreen.enabled = false
tmrchange.enabled = false
redlamp.fillcolor = black
yellowlamp.fillcolor = black
greenlamp.fillcolor = black
txtredtime.enabled = true
txtyellowtime.enabled = true
txtgreentime.enabled = true
cmdrun.caption = "啟動"
end if
end sub
private sub form_load()
tmrred.enabled = false
tmryellow.enabled = false
tmrgreen.enabled = false
tmrchange.enabled = false
red = &hff&
yellow = &hffff&
green = &hff00&
black = &h0&
lampbox.fillcolor = black
redlamp.fillcolor = black
yellowlamp.fillcolor = black
greenlamp.fillcolor = black
tmrchange.interval = 300
txtredtime.text = 6
txtyellowtime.text = 1
txtgreentime.text = 8
end sub
private sub tmrchange_timer()
static n as integer
n = n + 1
select case lampno
case 0
if redlamp.fillcolor = black then
redlamp.fillcolor = red
yellowlamp.fillcolor = yellow
greenlamp.fillcolor = green
else
redlamp.fillcolor = black
yellowlamp.fillcolor = black
greenlamp.fillcolor = black
end if
case 1
if redlamp.fillcolor = black then
redlamp.fillcolor = red
else
redlamp.fillcolor = black
end if
case 2
yellowlamp.fillcolor = yellow
case 3
if greenlamp.fillcolor = black then
greenlamp.fillcolor = green
else
greenlamp.fillcolor = black
end if
end select
if n = 4 then
n = 0
lampno = lampno + 1
if lampno = 5 then lampno = 1
select case lampno
case 1
redlamp.fillcolor = red
yellowlamp.fillcolor = black
greenlamp.fillcolor = black
tmrred.enabled = true
case 2, 4
redlamp.fillcolor = black
yellowlamp.fillcolor = yellow
greenlamp.fillcolor = black
tmryellow.enabled = true
case 3
redlamp.fillcolor = black
yellowlamp.fillcolor = black
greenlamp.fillcolor = green
tmrgreen.enabled = true
end select
tmrchange.enabled = false
end if
end sub
private sub tmrgreen_timer()
tmrchange.enabled = true
tmrgreen.enabled = false
end sub
private sub tmrred_timer()
tmrchange.enabled = true
tmrred.enabled = false
end sub
private sub tmryellow_timer()
tmrchange.enabled = true
tmryellow.enabled = false
end sub
2樓:匿名使用者
這些數字與timer的時間間隔計數有關:
綠燈亮3個計時單位,然後黃燈亮2個計時單位,然後紅燈亮3個計時單位,最後再亮黃燈1個計時單位,依次迴圈下去,按這個數下去就知道了。
******************************===因為select case 只用到個位數的1~9,而0沒有選擇,實際上就是和9是一致的,而如果用100取餘的話,那select case的情況,就是100(0~99)個了,但訊號燈的情況就3種,那樣就沒太大實際意義了,最多就是可以將每個的延時設定得變化更多些,用1000取餘就更不用說了。
實際的執行結果是:
綠燈亮3個計時單位(1~3)->黃燈亮2個計時單位(4,5)->紅燈亮3個計時單位(6~8)->黃燈亮2個計時單位(9,0)->再到綠燈.....不斷迴圈下去
VB程式設計題,VB程式設計練習題?
1.新建工程 工程1 2.新建一下combobox控制元件 combo1,並將屬性style改為2 3.新建五個label label1,label2,label3,label4,label5 4.新建兩個text文字框 text1,text2 5.新建一個commandbutton按鈕 comma...
VB程式設計題
五十分太少,十幾道題,太虧了,不做 蒼鬱 你把題分開,一道題一個帖子,就會有很多人幫你做了,都是些很簡單的題 五十分太多,十幾道題,太賺了,我做 1.在文字框text1中輸入一串字元,統計其中字母 不區分大小寫 數字和其他字元的個數。dim i as integer,a as integer,b a...
vb程式設計題?這道vb題怎麼寫程式碼?
private sub command1 click max val val text2.text val val val text5.text val val val text8.text val val min val val text2.text val val val text5.text ...