Simple Single Side Traffic Controller Using Timer Control

Introduction

Every coding person must wonder how the controls might be used in their application and what purpose they are going to serve. For such coders, I have shared my simple knowledge of using Timer control which is a basic but important control that can  be used in an effective way to showcase their application.
 
Procedure
 
The below given steps will help you to develop a Traffic Control application which works for single side direction.
 
Step 1

We need Visual Studio for developing our application, and in my case I have used VS 2010 for its simplicity.
 
Step 2

The user has to open up Visual Studio and select a fresh project using the steps in the below picture.
 
 
 
Step 3

By selecting a new project you will be opened up with a new window where all the developing templates are available as options. Here you can select C# Windows Form application and save the application name. After typing the application name select OK which will take you into the Windows work area. 
 
 
 
Step 4

After entering the Windows work area now we need the control ingredients to make our application into an interactive one. I have included the following controls in my form.
 
  • Picture Box (3 nos)
  • Label (1)
  • Text box (1)
  • Timer control (2 nos)
 
Step 5

To fill the picture box with some images I have picked specific traffic control pictures which are available on the Internet and used them inside my application.The below given image shows the sample pictures which I chose from the Internet for my use.

 

Step 6

The thing to be noted here is I have placed 3 picture boxes and they are placed one above the other and look like as if there is only one single picture box inside the form.
 
Step 7

The use of text box is to display some text while the signal is in a particular color. This is just to make the application more productive. The use of Label is to display the time for which a particular color in the signal would last. Since there are three different colors they have to be controlled according to time slot.
 
Step 8

The true magic of this application is with the use of timer control which i have used 2 here for two purposes. One is for making the image take turns in displaying and the other for displaying the counter number which runs in reverse order inside a label. The properties to be changed for the timer are given in the below picture. Thing to be noted is,
  • Timer --> enabled is true.
  • Timer --> interval is in thousands (x 1000) since one second is represented in 1000 milliseconds.

     

Step 9

After placing the controls in the right place inside the windows form and applying the below given code into the controls we could get the running traffic signal application without any errors.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9.   
  10. namespace WindowsFormsApplication1  
  11. {  
  12.     public partial class Form1 : Form  
  13.     {  
  14.         public int count=10;  
  15.         public int a;  
  16.   
  17.         public Form1()  
  18.         {  
  19.             InitializeComponent();  
  20.             a = 1;  
  21.              
  22.             pictureBox1.Visible = true;  
  23.             pictureBox2.Visible = false;  
  24.             pictureBox3.Visible = false;  
  25.             txt_msg.Text = "Turn OFF your engine";  
  26.         }  
  27.               
  28.                  
  29.         private void timer1_Tick(object sender, EventArgs e)  
  30.         {  
  31.             if (a == 1)  
  32.             {  
  33.                 count = 10;  
  34.                 pictureBox1.Visible = false;  
  35.                 pictureBox2.Visible = false;  
  36.                 pictureBox3.Visible = true;  
  37.                 txt_msg.Text = "Have a safe journey";  
  38.                 timer1.Interval = 10000;  
  39.                 a = 2;  
  40.             }  
  41.             else if (a == 2)  
  42.             {  
  43.                 count = 10;  
  44.                 pictureBox1.Visible = false;  
  45.                 pictureBox2.Visible = true;  
  46.                 pictureBox3.Visible = false;  
  47.                 txt_msg.Text = "slow down your vehicle";  
  48.                 timer1.Interval = 10000;  
  49.                 a = 3;  
  50.             }  
  51.             else if (a == 3)  
  52.             {  
  53.                 count = 10;  
  54.                 pictureBox1.Visible = true;  
  55.                 pictureBox2.Visible = false;  
  56.                 pictureBox3.Visible = false;  
  57.                 txt_msg.Text = "stop your vehicle";  
  58.                 timer1.Interval = 10000;  
  59.                 a = 1;  
  60.             }  
  61.         }  
  62.   
  63.         private void timer2_Tick(object sender, EventArgs e)  
  64.         {  
  65.              
  66.             lbl_red.Text = count.ToString();  
  67.             count = count - 1;  
  68.               
  69.         }  
  70.   
  71.         private void Form1_Load(object sender, EventArgs e)  
  72.         {  
  73.               
  74.         }  
  75.     }  
  76. }  
Step 10

Every signal when started up begins with red color as starting point. The same is given as startup in the given below picture as running application.

 

 
 
Step 11

After the default signal for a particular time the signal turns green and waits for a particular time. The below given pictures describes the result.

 
 
 
 
Step 12

The changes in the colors are because the change in images based on timer and the result is listed below. For sample timing I have used only 10 seconds. Readers of this article might use different timings for their usage.
 
 
 
 
 
 
 
Summary

The above procedure is for creating productive applications using various controls. Timer is one such concept where the user can create effectively. This application is only for a single side traffic control. In my next article I will create an application with four side controls.


Similar Articles