How to Program the Timer Control in VB.NET

Written by laura gittins
  • Share
  • Tweet
  • Share
  • Email

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:
Easy

Other People Are Reading

Instructions

  1. 1

    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.

  2. 2

    Press "F7" to open the code window, and type the following code:

    Dim time As Integer = 10

    This creates a variable and initialises it.

  3. 3

    Open the Timer1_Tick subroutine and type the following code:

    If time > 0 Then

    time = time - 1

    Label1.Text = time

    Else

    Timer1.Enabled = False

    End If

    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.

  4. 4

    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

    End If

    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.

  5. 5

    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

    TextBox1.Select(TextBox1.TextLength, 0)

    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.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.