Friday 2 March 2012

C# Timer Control in Windows Form

C# Timer Control in Windows Form.


Timer Control in C# is a control which generate events on timing of clock intervals in seconds, milliseconds and nanoseconds. This is the only control which is used to generate events automatically after a specified period of time (in seconds or minutes as user wants). Timer control has six properties in which interval property is used to set the interval of time in seconds, milliseconds etc. If a user assign the value of 1000 to interval property then the timer control generate event after one second. Here 1000 is equal to 1 second.

Let's take a example of Timer control in Windows Digital Clock.
1. Open Visual Studio 2010 IDE.
2. Select C# Windows Form Application.
3. After that, add three labels to your windows form, and assign the value 0 to text property of label control.
4. Add a Timer control from Toolbox. Set Enable property of Timer control to True and set value 1000 to         Interval property of Timer control in Properties tab.
5. Double Click on Timer control to create back end code where source code will be added to control for       completing the task.
6. When you double click on Timer control, a new window appears in front of you in IDE. Add this source
    code to that window and press F5 key or select run option in IDE to run the program.

Here the Source Code of Digital Clock:

      private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            label3.Text = Convert.ToString(Convert.ToInt32(label3.Text) + 1);
            if (label3.Text == "60")
            {
                label2.Text = Convert.ToString(Convert.ToInt32(label2.Text) + 1);
                label3.Text = "0";
                if (label2.Text == "60")
                {
                    label1.Text = Convert.ToString(Convert.ToInt32(label1.Text) + 1);
                    label2.Text = "00";
                    if (label1.Text == "24")
                    {
                        label3.Text = "00";
                        label2.Text = "00";
                        label1.Text = "00";
                    }
                }


            }

7. The first line of code shows that Timer control is enabled. Because if Timer control is disabled then Timer
    control cannot work.
8. Second line shows that label3 text (which is in string format) should be incremented, So to assign + 1
    value to label3 text we have converted label text to int format and after that again converts it into string
    format and stores incremented value into label3.Text.
9. You will see text on the label3 ( which is 0 ) increment after every one second. Like 1, 2, 3 and so on
     upto 60.Here label3 we have taken it as seconds in clock.
10. When the text of label3 reaches upto 59, then the text in label2 will be 1 and increment after every
       interval. Here label2 is for minutes and label1 is for hours in clock.

Here you have seen how can we use Timer control. Hope you enjoy it....

directory2009.com/add-url