Monday, October 29, 2012

How to use the Timer

How to use the Timer from System.Windows.Forms.Timer


There a many ways to do things. Today, let’s try to use the timer control. The timer control is more like a countdown function. Is simple and doesn’t slow down your application. In fact, it runs a new thread in background.
For this example, create a new project as a Windows Forms Applications.  Double-click the Timer from the tool box. This will insert the Timer1.
Now, the first thing you should do it to take a look at the code here.


Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Timer1.Interval = 1000
        Timer1.Enabled = True
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick
        Me.Text = Date.Now
    End Sub
End Class


The first function initiates the Timer1 and set some values. The interval is the period.  If the interval is a 1000, the interval is set a 1 seconds. You see? The units are in milliseconds.
Then, make sure to enable your timer. By default, is set to false. Make sure is set to true if you want the timer to work.

Finally, use the start button to start the count down.

At 1000 milliseconds, the program triggers the event and calls the function Timer1. Inside that function, place anything you want.

In this example, I made a simple clock.

This is very interesting if you need a timer in a little code in Excel or Word. Use it for simple program too.


The program I love to use:

Sample Code : TimerSample.zip

No comments:

Post a Comment