Interested on Microsoft technologies? Read Visual Studio Dot Net

Tutorial 1: The timer control and stop watch application

Reason of this project:
Many of beginners’ programmers don’t know the timer control or how to use it to fit their needs.
For this reason, I came with this tutorial to show what the timer control is and how to use it on our application.

Project details:
1- Description of the timer control
2- The timer example
3- The speed watch example
4- The alarm example

1- Description of the timer control:


The timer control isn’t a timer and don’t show time. We use to create timer and speed watch.
How it function?
Simply, the timer control counts a period of time and then does an event.
  • You set an interval for the timer (for example 1 second)
  • You start the timer
  • The timer start counting
  • 1 Second: The timer calls an event (that you have declared)
  • The event does an action

--> And the timer loop (every 1 second).

We can say that the timer is a regular loop that does an event every interval of time that you set.

If you ask Visual Studio to get the MSDN help it'll shows:
The length of the intervals is defined by the Interval property, whose value is in milliseconds.
When the component is enabled, the Tick event is raised every interval.
This is where you would add code to be executed.
For more information, see How to: Run Procedures at Set Intervals with the Windows Forms Timer Component.
The key methods of the Timer component are Start and Stop, which turn the timer on and off.
When the timer is switched off, it resets; there is no way to pause a Timer component.

Many don't understand the MSDN help this is because you lack experience. The MSDN documentation thinks that you know every thing!
But I'll explain on an easy way how to use it and understand all this text.
To understand we'll do the first sample together, I'll explain as much as possible.
For the 2 other samples just read the comments associated to the code source.

2- The timer example


In this example we’ll create a clock that shows time. I know that every one can use the following expression: Date.now and get the time.
But now we won’t to update it, I mean every 1 second the clock refresh and show the time again.
If we use a loop this we’ll take much time of the computer process, plus the application we’ll not be stable and we’ll need threading…
With the timer control we can do the clock with less problems and code lines.
You can follow the steps to create the project or just read and see the source code.
First create a new project and name it clock.
Then take a clock control and a label control (only 2 controls)
And now double click over the form and you should be in the Form Load part.
First we need to set the timer interval. For our clock: 1 second.
Timer1.Interval = 1000

Now let’s start the timer. From this line the timer will start counting.
Timer1.Start()

Now create a sub that the counter will call.
Private Sub count(ByVal MyObject As Object, _
ByVal myEventArgs As EventArgs)
Dim time As String
time = Date.Now.Hour & ":" & Date.Now.Minute & ":" & Date.Now.Second
Label1.Text = time
End Sub

The ByVal MyObject As Object and the ByVal myEventArgs As EventArgs are elementar!

Now we have to return to the form load. We need to make a relation between the timer and the sub.
So before all (starting the timer and setting the interval), add the following line.
AddHandler Timer1.Tick, AddressOf count


All is done! Now run the project.Run the project. You should see the time update every second like the Windows clock.


3- The Stop watch example

I think you understood the timer control system. So I'll give only a description of this example.
For more help read the comments to understand it better.
The speed watch as it's name, is a speed watch =)
Example aim : one button to start and stop the watch. Another button to reset.
The user can specify the interval, so another button to set the interval.

4- The alarm example

This is a clock example! But we'll added a new function: The Alarm.
You set a time where you want to show a message. When the timer control reach the time, it call the event.
For more details see the project comments.

Download the source code

The Zip file contains:
-The 3 examples source code
-The readme.txt file
-The tutorial.txt file

Still have question:
If you have any problem or you found a bug, post a comment describing your problem.
If you have a general question, we highly recommend the MSDN Forums as the best Dot Net forums in the net.

Bookmark and share this tutorial
Related Ads

0 comments:

Related articles