How to Use Timer1 on VB.NET
Time, critical in many areas of life, often plays a vital role in software development as well. Some games, for example, might consist of modules that need to know the exact time down to the millisecond. VB.NET developers have access to a useful .NET control called the "Timer." Simply drag one of these controls onto your project's form, and .NET names it Timer1 if no other timers exist. Your application can then use Timer1 to trigger events at intervals you specify.
Instructions
-
-
1
Launch Visual Studio. Click the "File" menu, and then click "New" to display a list of options. Click the "Project" option, and then click "Visual Basic."
-
2
Double-click "Windows Forms Application." Visual Studio creates a new Windows forms project. A new form appears in the project's Design window.
-
-
3
Click the "View" menu, and then click "ToolBox" to view Visual Studio's toolbox. Locate the "Timer" control in the toolbox, and double-click that control. A control named Timer1 will appear below the form at the bottom of the screen.
-
4
Right-click "Timer1," and then click "Properties" to view the timer's Properties window. Click the "Arrow" shaped icon at the top of the window. The "Tick" event icon appears. Double-click that icon. Visual Studio opens the code window and displays the "Timer1_Tick" sub. This sub runs every time Timer1 ticks. You determine how often the timer ticks by setting its "Interval" property.
-
5
Paste the following code into before the sub's "End Sub" statement:
Timer1.Stop()
MessageBox.Show("Tick event triggered. Tick Counter = " & tickCounter.ToString())
If tickCounter < tickCounterLimit Then
Timer1.Start()
tickCounter = tickCounter + 1
End If
This code --- which runs when Timer1 ticks --- stops the timer and opens a message box. The message box displays the value of a variable named tickCounter. The "If" statement after the MessageBox.Show statement restarts the timer if the value of tickCounter is less than the time limit you set in the variable named tickCounterLimit. The code then adds 1 to the tickCounter variable.
-
6
Move to the top of the code window, and paste the code shown below above all other statements in the code window:
Private tickCounter
Private timerInterval
Private tickCounterLimit
These statements define three variables used by other subs in the application.
-
7
Press F7. The form reappears in the code window. Double-click the form's title bar at the top of the form. The code window opens and displays the "Form1_Load" sub. This runs when your form loads.
-
8
Paste the following code before this sub's "End Sub" statement:
tickCounter = 1
timerInterval = 2000
tickCounterLimit = 3
Timer1.Interval = timerInterval
Timer1.Start()
The first statement sets the value of the tickCounter variable to 1. The timerInterval variable holds the value "2000" in this example. That is the number of milliseconds the timer will wait before executing the "Timer1_Tick" sub. The tickCounterLimit variable sets a limit on the number of times the code will call the "Timer1_Tick" sub. The Timer1.Start method starts the timer.
-
9
Press "F5." to run the project. The form appears, and the timer starts ticking. After 2000 milliseconds elapse, the code inside the Timer1_Tick sub runs. This code displays a message box that shows the value of the tickCounter variable. This value is 1.
-
10
Press "Enter" to close the message box. The code resumes execution, increments the tick Counter variable by 1 and restarts the timer. 2000 milliseconds later, the message box reappears and displays the tickCounter variable's value that now reads 2.
-
11
Press "Enter" a final time. The message box shows 3 for the variable's value. Since you set the value of tickCounterLimit to 3, the timer does not restart.
-
1
Tips & Warnings
Keeping track of a variable such as tickCounter is an excellent way to count the number of times your timer ticks. You could even build a digital clock by setting the timer's interval to 1000 milliseconds and incrementing the clock's hands by 1 whenever a tick occurred. Reminder applications also use timers. For example, you could allow a user to define a time interval of 60,000 --- or six minutes. After that interval elapses, the Timer1_Tick sub could sound an alarm letting the user know that time is up.
References
Resources
- Photo Credit Burke/Triolo Productions/Brand X Pictures/Getty Images