C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Visual Studio »

Adding a Timer control in Visual Basic


Posted Date: 26 Oct 2008    Resource Type: Code Snippets    Category: Visual Studio
Author: RaghavMember Level: Gold    
Rating: 1 out of 5Points: 9



A Timer control in Visual Basic is used for setting the time interval to execute any event after that interval. This is very useful if we want to perform some operations after a certain time. Say you want to create a backup of your data processing in every hour. You can make a routine which will take the backup and call that routine on Timer's event and set timer interval for an hour.

The code snippet given below gives an example of a timer that prints off a line after every second into a list box.

In this example we need 4 command buttons with the following names/captions.

#1 Command Button:
Name: cmdStart
Caption: Start Timer

#2 Command Button:
Name: cmdStop
Caption: Stop Timer

#3 Command Button:
Name: cmdClear
Caption: Clear Text

#4 Command Button:
Name: cmdExit
Caption: Exit


1 ListBox
With:

Name:lstCount
Caption: (None)

1 Timer
With:

Name: tmrTimer
Caption: (None)


The Code is given below:


Add this code to General Declarations:
{CODE}
Dim Response As Integer
Dim mintCount As Integer


Then this:


Private Sub cmdClear_Click()
lstCount.Clear
End Sub

Private Sub cmdExit_Click()
Response = MsgBox("Exit Program?", vbInformation + vbYesNo, "Are you sure?")
If Response = vbYes Then
Unload Me
ElseIf Response = vbNo Then
Me.Refresh
End If
End Sub

Private Sub cmdStart_Click()
mintCount = 0
Cls
tmrTimer.Enabled = True
End Sub


Private Sub cmdStop_Click()
tmrTimer.Enabled = False
End Sub

Private Sub tmrTimer_Timer()
mintCount = mintCount + 1
lstCount.AddItem "Timer fired again. Count = " & mintCount
End Sub


Whenever you click the "Clear Text" button, the ListBox will be cleared of all text.

If you click the "Exit" button, it asks you if you REALLY want to exit. If YES then it will exit, if NO then it will just go back to the previous screen.

The timer will start everytime you click on "Start Timer" button.
The timer will stop when ever you click on "Stop Timer" Button

Finally the text "Timer fired again. Count = ?" is added into the ListBox every time.

Hope this is very useful to all members

Raghav




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Adding Timer in VB  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How to add a windows control library template to Visual C# Express
Previous Resource: Using Required Field Validator
Return to Discussion Resource Index
Post New Resource
Category: Visual Studio


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use