Traffic Lights

 

This is a Visual Basic program that works like the actual traffic lights. In this program, you have to insert one timer and set its interval to any figure. Here we set it to 3000, which is equivalent to 3 seconds. Next you insert three shapes and set their shape properties to circle and fill them with three colors, green, amber (or yellow) and red. Use the following code to set one color on and the other two colors off:

Shape2.Visible = True
Shape1.Visible = False
Shape3.Visible = False

 

The Interface

 

 

The Code

Private Sub Timer1_Timer()
If Shape1.Visible Then

Shape2.Visible = True
Shape1.Visible = False
Shape3.Visible = False

ElseIf Shape2.Visible Then

Shape3.Visible = True
Shape2.Visible = False
Shape1.Visible = False

Else

Shape1.Visible = True
Shape2.Visible = False
Shape3.Visible = False

End If

End Sub

 

 


 

[Back to VB Sample Codes]

Copyright ® 2008 Dr.Liew Voon Kiong . All rights reserved |Contact: admin@vbtutor.net

[Privacy Policy]