Monday, January 11, 2010

Timer Control in Vb6

What is a timer control



A timer is a timer. You can place a timer control and give an interval in milliseconds. After the lapse of specified interval timer's Timer() event will fire. You can write code in this block. In other words if you want to execute a certain code at certain interval you can use timer on a windows form. The timer event will continue to fire unless you change the Timer's enabled property to False.

Now we will see the timer functionality with an example tutorial. In this tutorial we will put a timer control on a windows form to display current date and time.

Timer Tutorial Project

Create a standard EXE project and put a Timer control on the form. Set its name to tmrTime. Set the Timer's Enabled property to True. Set the interval property to 1000 (milliseconds). Place a label control on the form and set it's Name property to lblDate. Put another label control and set its name property to lblTime.

Double click the Timer control and write the following code,

Private Sub tmrTime_Timer()
lblDate.Caption = Date
lblTime.Caption = Time()
End Sub

You can format the Date Time as per your requirement. For Date Formatting,
use Format(Date,"dd-MMMM-yy" ) to display date in day-month-year format.

To format Time use, Format(Time(),"hh:mm AMPM") will display hour:minute AM/PM

Run the project by pressing F5 key


Hurray! You have created a simple project to display current date and time.