How to Create a Simple Traffic Lights in VB.net

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.

Creating Application

Step 1

Open Microsoft Visual Basic 2015 and create a new windows form application. trafic123

Step 2

Do the form just like shown below. trafic231

Step 3

Double click the timer to fire the tick event handler of it. Then, add the following codes for changing the traffic lights.
  1. Select Case Cnt
  2. Case 0
  3. If Timer1.Interval = 2000 Then
  4. TextBox1.BackColor = Color.White
  5. TextBox2.BackColor = Color.White
  6. TextBox3.BackColor = Color.FromArgb(0, 255, 0)
  7.  
  8. Cnt = 1
  9. Else
  10. Timer1.Interval = Timer1.Interval + 200
  11. End If
  12. Case 1
  13.  
  14. If Timer1.Interval = 3000 Then
  15. TextBox3.BackColor = Color.White
  16. TextBox2.BackColor = Color.White
  17. TextBox1.BackColor = Color.FromArgb(255, 0, 0)
  18. Timer1.Interval = 1000
  19. Cnt = 2
  20. Else
  21. Timer1.Interval = Timer1.Interval + 200
  22. End If
  23. Case 2
  24. If Timer1.Interval = 4000 Then
  25. TextBox3.BackColor = Color.White
  26. TextBox1.BackColor = Color.White
  27. TextBox2.BackColor = Color.Yellow
  28. Timer1.Interval = 1000
  29. Cnt = 0
  30. Else
  31. Timer1.Interval = Timer1.Interval + 200
  32. End If
  33.  
  34.  
  35. End Select
  36. Me.Text = Now()

Step 4

Do the following codes to start changing traffic lights when the button is clicked.
  1.  
  2. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  3. Cnt = 0
  4. Timer1.Start()
  5. Timer1.Interval = 2000
  6. End Sub
For any questions about this article. You can contact me @ Email – [email protected] Mobile No. – 09305235027 – TNT Or feel free to comment below.

Add new comment