In Visual Basic .NET, the Timer control lets you control recurring events during run time for your application. Timer has few properties and methods that are unique to it. Its most important property is Interval, which specifies how often the Timer should execute its code. You supply an Interval value in milliseconds; 1,000 milliseconds equals one second. Some examples of what you can do with a Timer control include performing a countdown, dynamically changing property values for other controls and maintaining a constant watch on some other part of the application.
- Skill level:
Other People Are Reading
Open a Visual Basic project. Double-click the Timer control to add Timer 1 to the project. Double-click the Label control to add Label 1 to the form. Change the Timer 1 control's Enabled property to True and the Interval property to 1000.
Press "F7" to open the code window, and type the following code:
Dim time As Integer = 10
This creates a variable and initialises it.
Open the Timer1_Tick subroutine and type the following code:
If time > 0 Then
time = time - 1
Label1.Text = time
Timer1.Enabled = False
When the program runs, the Timer will decrement the time variable by one every second, and display the value on the label, until the count reaches 0, at which point the Timer turns off.
Press "Shift" and "F7" to open the design window. Double-click the Timer control to add Timer 2 to the project. Change its Enabled property to True and its Interval property to 100, then double-click it to open the Timer2_Tick subroutine. Type the following code:
If (Label1.Left + Label1.Width) > Me.Width Then
Label1.Left = Me.Left - 100
Label1.Left = Label1.Left + 5
This code causes the label to scroll horizontally across the screen, moving five pixels every tenth of a second. When the label reaches the right end of the form it jumps back to the left and begins scrolling across again.
Press "Shift" and "F7" to open the design window. Double-click the Timer control to add Timer 3 to the project. Change its Enabled property to True and the Interval property to 500. Double-click the TextBox control to add TextBox1 to the project. Double-click Timer 3 on the project to open the Timer3_Tick subroutine. Type the following code:
TextBox1.Text = TextBox1.Text.ToUpper
Every half-second the Timer changes all the text in TextBox1 to upper case. By default, when the Timer makes this change it moves the cursor back to the beginning of the text box. The second line of code prevents this from happening.
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for