Wednesday, December 29, 2010

Using the Timer class

Great... I accidentally deleted all previous posts so I can start all over again.


I'm trying to get my head around delegates and events and while doing this, i stumbled across the Timer class. What I tried to accomplish was to create a simple stop watch application to better understand the principles of delegates and events. After some studying, I noticed that I was mixing up the Stopwatch and Timer class. So this made me decide to focus solely on the Timer class for now. Also to keep the whole delegates and events happening really simple.

So, how can we define the Timer class? According to MSDN:

"Provides a mechanism for executing a method at specified intervals. This class cannot be inherited."

I don't think there is a more clear definition than that... which actually suprises me because a t times i feel reading the MSDN documentation is like reading Japanese. Ok, to put this in practice I created the following:

  • a simple form
  • a button called 'button1' (which is the default name for the first button you drag-n-drop), with the text 'Start'
  • an empty textbox, called 'txtShowTime'

This is how I set up the Timer (by code):

Go the the Form1.Designer.cs code, look for the following lines and add:

private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox txtShowTime;
private System.Windows.Forms.Timer tickTock; // add, declares a Timer variable

At the very top of the InitializeComponent() look for the following lines and add:

this.button1 = new System.Windows.Forms.Button();
this.txtShowTime = new System.Windows.Forms.TextBox();
this.components = new System.ComponentModel.Container();
this.tickTock = new System.Windows.Forms.Timer(this.components); // add, initializes an instance of type Timer

Now, anywhere in the InitializeComponent(), add the following lines:

this.tickTock.Interval = 250; // sets the frequency  of the event in millisecs
this.tickTock.Enabled = false; // disabling the timer per default
this.tickTock.Tick += new System.EventHandler(tickTock_Tick); // creating event handler

At this point, we have to create a method to which the event handler is pointing to ('tickTock_Tick'). Create this in Form1.cs:

private void tickTock_Tick(object sender, EventArgs e)
   txtShowTime.Text = DateTime.Now.ToString(); // displays the time in the textbox

So, every 0,25 sec the tickTock_Tick method is called and will display/refresh the time in the textbox. 

To initialize the displaying of time on the form, we will add some functionality to the Button1. Look for it in Form1.Designer.cs and change as follows:

this.button1.Location = new System.Drawing.Point(13, 13);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Start";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click +=new System.EventHandler(button1_Click); // add this to create an event handler upon clicking the button

Same story here as for the Tick event; we need to create a method in Form1.cs named 'button1_Click'. In this method we'll add some simple functionality which will start the timer and change the text of the button. Alright, go to Form1.cs and add the following code right after the tickTock_Tick method:

private void button1_Click(object sender, EventArgs e)
if (tickTock.Enabled == true)
    tickTock.Enabled = false;
           button1.Text = "Go!";
       tickTock.Enabled = true;
       button1.Text = "Stop";

Run the app and test.

No comments:

Post a Comment