SiteMap  | About    

Timer Control - VB.Net

What is Timer Control ?

Timer Control plays an important role in the Client side programming and Server side programming, also used in Windows Services. By using this Timer Control, windows allow you to control when actions take place without the interaction of another thread. timer control

Use of Timer Control

We can use Timer Control in many situations in our development environment. If you want to run some code after a certain interval of time continuously, you can use the Timer control. As well as to start a process at a fixed time schedule, to increase or decrease the speed in an animation graphics with time schedule etc. you can use the Timer Control. The Visual Studio toolbox has a Timer Control that allowing you to drag and drop the timer controls directly onto a Windows Forms designer. At runtime it does not have a visual representation and works as a component in the background. timer object

How to Timer Control ?

With the Timer Control, we can control programs in millisecond, seconds, minutes and even in hours. The Timer Control allows us to set Interval property in milliseconds (1 second is equal to 1000 milliseconds). For example, if we want to set an interval of two minute we set the value at Interval property as 120000, means 120x1000 .

The Timer Control starts its functioning only after its Enabled property is set to True, by default Enabled property is False. timer property

Timer example

The following program shows a Timer example that display current system time in a Label control. For doing this, we need one Label control and a Timer Control. Here in this program, we can see the Label Control is updated each seconds because we set Timer Interval as 1 second, that is 1000 milliseconds. After drag and drop the Timer Control in the designer form , double click the Timer control and set the DateTime.Now.ToString to Label control text property.

Public Class Form1 Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Label1.Text = DateTime.Now.ToString End Sub End Class

Start and Stop Timer Control

We can control the Timer Control Object that when it start its function as well as when it stop its function. The Timer Control has a start and stop methods to perform these actions. timer property

Here is an example for start and stop methods of the Timer Control. In this example we run this program only 10 seconds. So we start the Timer in the Form_Load event and stop the Timer after 10 seconds. We set timer Interval property as 1000 milliseconds ( 1 second) and in run time the Timer will execute 10 times its Tick event .

Public Class Form1
    Dim second As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Interval = 1000
        Timer1.Start() 'Timer starts functioning
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Label1.Text = DateTime.Now.ToString

        second = second + 1
        If second >= 10 Then
            Timer1.Stop() 'Timer stops functioning
            MsgBox("Timer Stopped....")
        End If

    End Sub

End Class (C) 2016    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.