This Season
 

How to Program the Timer Control in VB.NET

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.

Related Searches:
    Difficulty:
    Easy

    Instructions

      • 1

        Open a Visual Basic project. Double-click the Timer control to add Timer1 to the project. Double-click the Label control to add Label1 to the form. Change the Timer1 control's Enabled property to True and the Interval property to 1000.

      • 2

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

        Dim time As Integer = 10

        This creates a variable and initializes it.

      • 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

        Press "Shift" and "F7" to open the design window. Double-click the Timer control to add Timer2 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

        Press "Shift" and "F7" to open the design window. Double-click the Timer control to add Timer3 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 Timer3 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 uppercase. 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.

    Related Searches

    References

    Read Next:

    Comments

    You May Also Like

    Follow eHow

    Related Ads