How to Create a Simple Traffic Lights in VB.net
Submitted by janobe on Friday, October 19, 2018 - 21:46.
The tutorial that I’m going to teach you today is about creating a simple traffic lights in VB.Net. This is a simple program that can be done in an hour or less. A time interval is set in this program so it will work like the actual traffic lights. To see how it works, use the following code below.
For any questions about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment below.
Creating Application
Step 1
Open Microsoft Visual Basic 2015 and create a new windows form application.
Step 2
Do the form just like shown below.
Step 3
Double click the timer to fire thetick event handler
of it. Then, add the following codes for changing the traffic lights.
- Select Case Cnt
- Case 0
- If Timer1.Interval = 2000 Then
- TextBox1.BackColor = Color.White
- TextBox2.BackColor = Color.White
- TextBox3.BackColor = Color.FromArgb(0, 255, 0)
- Cnt = 1
- Else
- Timer1.Interval = Timer1.Interval + 200
- End If
- Case 1
- If Timer1.Interval = 3000 Then
- TextBox3.BackColor = Color.White
- TextBox2.BackColor = Color.White
- TextBox1.BackColor = Color.FromArgb(255, 0, 0)
- Timer1.Interval = 1000
- Cnt = 2
- Else
- Timer1.Interval = Timer1.Interval + 200
- End If
- Case 2
- If Timer1.Interval = 4000 Then
- TextBox3.BackColor = Color.White
- TextBox1.BackColor = Color.White
- TextBox2.BackColor = Color.Yellow
- Timer1.Interval = 1000
- Cnt = 0
- Else
- Timer1.Interval = Timer1.Interval + 200
- End If
- End Select
- Me.Text = Now()
Step 4
Do the following codes to start changing traffic lights when the button is clicked.- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- Cnt = 0
- Timer1.Start()
- Timer1.Interval = 2000
- End Sub