『壹』 VB编程设计题:红绿灯

如图在窗体上添加:

1、四个shape控件,作为灯框、红灯、黄灯、绿灯,分别命名为:LampBox、RedLamp、YellowLamp、GreenLamp

2、一个命令按钮,命名为:cmdRun,标题为“启动”

3、三个文本框控件,来设置红灯、黄灯、绿灯亮的时间(秒),分别命名为:txtRedTime、txtYellowTime、txtGreenTime

4、四个时钟控件,作为红灯、黄灯、绿灯和闪烁时间控制,分别命名为:tmrRed、tmrYellow、tmrGreen、tmrChange

5、其它控件和布局见图

程序如下:

PrivateRedAsLong

PrivateYellowAsLong

PrivateGreenAsLong

PrivateBlackAsLong

PrivateLampNoAsInteger

PrivateSubcmdRun_Click()

IfcmdRun.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="启动"

EndIf

EndSub

PrivateSubForm_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

EndSub

PrivateSubtmrChange_Timer()

StaticnAsInteger

n=n+1

SelectCaseLampNo

Case0

IfRedLamp.FillColor=BlackThen

RedLamp.FillColor=Red

YellowLamp.FillColor=Yellow

GreenLamp.FillColor=Green

Else

RedLamp.FillColor=Black

YellowLamp.FillColor=Black

GreenLamp.FillColor=Black

EndIf

Case1

IfRedLamp.FillColor=BlackThen

RedLamp.FillColor=Red

Else

RedLamp.FillColor=Black

EndIf

Case2

YellowLamp.FillColor=Yellow

Case3

IfGreenLamp.FillColor=BlackThen

GreenLamp.FillColor=Green

Else

GreenLamp.FillColor=Black

EndIf

EndSelect

Ifn=4Then

n=0

LampNo=LampNo+1

IfLampNo=5ThenLampNo=1

SelectCaseLampNo

Case1

RedLamp.FillColor=Red

YellowLamp.FillColor=Black

GreenLamp.FillColor=Black

tmrRed.Enabled=True

Case2,4

RedLamp.FillColor=Black

YellowLamp.FillColor=Yellow

GreenLamp.FillColor=Black

tmrYellow.Enabled=True

Case3

RedLamp.FillColor=Black

YellowLamp.FillColor=Black

GreenLamp.FillColor=Green

tmrGreen.Enabled=True

EndSelect

tmrChange.Enabled=False

EndIf

EndSub

PrivateSubtmrGreen_Timer()

tmrChange.Enabled=True

tmrGreen.Enabled=False

EndSub

PrivateSubtmrRed_Timer()

tmrChange.Enabled=True

tmrRed.Enabled=False

EndSub

PrivateSubtmrYellow_Timer()

tmrChange.Enabled=True

tmrYellow.Enabled=False

EndSub