Go4Expert (http://www.go4expert.com/)
-   Visual Basic [VB] (http://www.go4expert.com/articles/visual-basic-tutorials/)
-   -   Perform periodic operations with VB.Net's Timer control (http://www.go4expert.com/articles/perform-periodic-operations-vbnets-t3643/)

pradeep 27Mar2007 17:41

Perform periodic operations with VB.Net's Timer control
Developers are often required to make a specific action take place on a form on a regular basis or after a set time interval. One way you can achieve this functionality is by using VB.NET's Timer control.


One of the best ways to understand how the Timer control works is with an example. Follow these steps:

1. Add a Timer control (which is available under the Components section) to your form and call it Timer1.
2. Set its Interval property to 1000 and its Enabled property to True.
3. Add a Label control to your form, call it lblDateTime, and set its Text property to "".
4. Add the following code:

Code: VB

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

When you run the code, the date/time value displayed on the form is updated periodically.


The Timer1_Tick event is fired once the time specified in the Interval property passes. Since the code I execute on this event simply updates the lblDateTime's Text property with the current Date/Time value, the Date/Time displayed on the form is updated every 1,000 milliseconds (as specified in the Timer1's Interval property).

All times are GMT +5.5. The time now is 19:23.