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 6The 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.
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
-
- namespace WindowsFormsApplication1
- {
- public partial class Form1 : Form
- {
- public int count=10;
- public int a;
-
- public Form1()
- {
- InitializeComponent();
- a = 1;
-
- pictureBox1.Visible = true;
- pictureBox2.Visible = false;
- pictureBox3.Visible = false;
- txt_msg.Text = "Turn OFF your engine";
- }
-
-
- private void timer1_Tick(object sender, EventArgs e)
- {
- if (a == 1)
- {
- count = 10;
- pictureBox1.Visible = false;
- pictureBox2.Visible = false;
- pictureBox3.Visible = true;
- txt_msg.Text = "Have a safe journey";
- timer1.Interval = 10000;
- a = 2;
- }
- else if (a == 2)
- {
- count = 10;
- pictureBox1.Visible = false;
- pictureBox2.Visible = true;
- pictureBox3.Visible = false;
- txt_msg.Text = "slow down your vehicle";
- timer1.Interval = 10000;
- a = 3;
- }
- else if (a == 3)
- {
- count = 10;
- pictureBox1.Visible = true;
- pictureBox2.Visible = false;
- pictureBox3.Visible = false;
- txt_msg.Text = "stop your vehicle";
- timer1.Interval = 10000;
- a = 1;
- }
- }
-
- private void timer2_Tick(object sender, EventArgs e)
- {
-
- lbl_red.Text = count.ToString();
- count = count - 1;
-
- }
-
- private void Form1_Load(object sender, EventArgs e)
- {
-
- }
- }
- }
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.