Home » Programming » VB.NET » Story Details
Printable Version

VB.NET Timer

by Mahesh Chand on Nov 22, 2011

A Timer control raises an event at a given interval of time without using a secondary thread. If you need to execute some code after certain interval of time continuously, you can use a timer control.
Comments: 0    Views: 398

Timer Control

Timer control raises an event at a given interval of time without using a secondary thread. If you need to execute some code after certain interval of time continuously, you can use a timer control.

In this article, I will discuss how to create and use a Timer control in a Windows Forms application using Visual Studio 2010.

Timer Properties

Enabled property of timer represents if the timer is running. We can set this property to true to start a timer and false to stop a timer.

Interval property represents time on in milliseconds, before the Tick event is raised relative to the last occurrence of the Tick event. One second equals to 1000 milliseconds. So if you want a timer event to be fired every 5 seconds, you need to set Interval property to 5000.

Dim Timer1 As New Timer()

Timer1.Interval = 2000

Timer1.Enabled = True

Creating a Timer

A Timer control does not have a visual representation and works as a component in the background.

Design-time

You can create a timer at design-time by dragging and dropping a Timer component from Toolbox to a Form. After that, you can use F4 or right click Properties menu to set a Timer properties as shown in Figure 1. As you can see in Figure 1, the Enabled property is false and Interval is set to 1000 milliseconds (1 second).

TimerImg1.jpg
Figure 1

First thing you want to do is, change Enabled to true so the timer will start when your application starts.

Now next step is to add an event handler. If you go to the Events window by clicking little lightning icon, you will see only one Tick event as you can see from Figure 2. Double click on it will add the Tick event handler.

TimerImg2.jpg
Figure 2

Now whatever code you write on this event handler, it will be executed every 1 second. For example, if you have a ListBox control on a Form and you want to add some items to it, the following code will do so.

Private Sub Timer1_Tick(ByVal sender As System.Object, _

                            ByVal e As System.EventArgs) _

                        Handles Timer1.Tick

        ListBox1.Items.Add(DateTime.Now.ToLongTimeString() + "," + _

                           DateTime.Now.ToLongDateString())

End Sub

 

Run-time

Timer class represents a Timer control and used to create a Timer at run-time. The following code snippet creates a Timer at run-time, sets its property and event handler.

Dim t As New Timer()

t.Interval = 2000

t.Enabled = True

AddHandler t.Tick, AddressOf TimerEventHandler

 

The event handler code looks like following.

Private Sub TimerEventHandler(ByVal obj As Object, _

                                           ByVal ergs As EventArgs)

        ListBox1.Items.Add(DateTime.Now.ToLongTimeString() + "," + _

                         DateTime.Now.ToLongDateString())

End Sub

 

Summary

In this article, we discussed discuss how to create a Timer control in Windows Forms and set its various properties and events.

Post a Comment
*
DevExpress PowerBuilder Web Development Windows Development Languages Software Engineering Databases
iPhone Architecture Secutiry UML & Modeling Operating Systems Networking Testing
Graphics Design Project Management Hardware Open Source Games Development Business Intelligence Visual Studio LightSwitch 2011
MonoDevelop Visual Studio 2010 ASP.NET HTML, DHTML XML PHP JavaScript
Silverlight Web Services WCF Windows Forms WPF Windows Services Dynamic Link Libraries
ActiveX COM, DCOM, ATL C# VB.NET C++ F# Java
Pascal SQL Server Oracle DB2 MS-Access Windows Servers Windows
Linux Unix SAP LINQ .NET Framework ADO.NET Reporting
Crystal Reports SQL Server Reporting Services Igenda Reports Active Reports Adobe Fireworks Arrays & Collections Hosting
Future Trends Android Windows Phone Smart Devices Business M&A Investment & Funding
Web Browsers Internet Explorer Firefox Safari Common Entrepreneurs Students
Consulting Wiki Gadgets MobileMe iCloud iOS Social Media
Facebook Twitter LinkedIn Google+ Microsoft Kinect XBox
Wii Playstation DirectX i OS OS X CIO, CTO, CEO Windows 8
Web Design Expression Blend 4 Photoshop CS5 Creative Suite 5.5 Expression Web 4 Expression Studio 4 Creative Suite® 5.5 Design
Creative Suite 5.5 Web Creative Suite 5.5 Production Startups Funding M&A Laptops Smart Phones
Desktops Cameras & Camcorders Netbooks Tablets Virtualization Microsoft Surface WordPress
Software Products Cloud Computing Current Affairs Technology TV TV
Earnings XAML E-Commerce MonoTouch Mono for Android Deals Electronics
Mobile Phone Laptop Tablet Book Computer Press Releases Reviews
Products Books Companies Windows Azure SQL Azure Interviews Mac
Web Browsers Symbian Windows Forms WPF Windows Services HTML 5 Office 365
SharePoint 2010 Exchange Server Adobe Visual Studio 2012 iPad Flex / Flash Games
Windows 9
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id / Email:
Password:  
Forgot Password | Forgot UserName