Video Lectures‎ > ‎

Animation In VB

Use of Timer In Visual Basic

The Timer control in Visual Basic is a very useful device that sends a pulse to an application at pre-determined intervals. The control can display updates of how long an install is taking by counting down, monitor what a user is doing or execute other routines while a main program is running.

  • Open Visual Basic and select as "New Project" a "Standard EXE." Click "File" and "Save form1 as" and give it a name like "timerproject." Then click "Save project as" and name it "Timer Project." Visual Basic applies the correct file extensions by default.

  • 2

    Change the "Caption" property in the Properties panel on the lower right to "Timer Control."

  • 3

    Place a Timer Control on the form by double-clicking the "Timer" control, which appears as a clock icon, in the Toolbox in the left panel. The control will not be visible when the program runs. For clarity, click and drag it to a lower corner of the form.

  • 4
    Timer Form

    Put a label on the form by double-clicking the "Label" control, represented by a large letter "A" in the Toolbox. Click the "Label" now on the form, and then in the Properties panel change the name of the label to "Clock" and delete the letters in the caption line. Repeat this for a second label and name it "Greeting." Change the width for both labels to 2000.

  • 5
    Time Code

    Click View > Code at the top to open a code window. Write the code for a timer event. This is just an example; to control your desired timer uses you need to modify the code to do what you want it to do:
    Private Sub Form_Load()
    Timer1.Interval = 5000 ' numbers represent milliseconds so 5000 equals once every 5 seconds; change this to desired period
    Call Timer1_Timer
    End Sub
    Private Sub Timer1_Timer()
    Dim strGreeting1, strGreeting2, strTime As String
    strTime = Time$
    Clock.Caption = Time$
    strGreeting = "Hello There!"
    If Mid$(strTime, 7, 1) = "1" Then
    Greeting.Caption = strGreeting
    Else: Greeting.Caption = ""
    End If
    End Sub

  • 6

    Test the coding by pressing "F5." The current time should display and update every 5 seconds. When the first digit of the seconds display falls between 10 and 19, the words "Hello There!" will display and then vanish for 50 seconds. If neither of these events happens, check the code in Step Five for accuracy.

  • 7

    Turn this into a final program by clicking on File > Make Timer Project.exe.
    Run the program from wherever you saved it.

  • Reference: