Visual Basic Timer: How to Use It to Schedule Your Tasks

Visual Basic TimerVisual Basic is an important and user friendly programming language. It comes with a repertoire of controls which have properties related to their appearance and behavior. Today we look at the useful and important Visual Basic timer control, in this beginner’s level tutorial.

We assume in this tutorial that you already know the basics of programming. If not, we suggest that you go learn the basics of a programming language such as C or JavaScript. This course can help show you how.

What is Visual Basic?

Visual Basic is an event driven programming language and integrated development environment developed by Microsoft. It is based on the BASIC (Beginners All-Purpose Symbolic Instruction Code) language. It was one of the first products to introduce GUI or Graphical User Interface. Instead of focusing on syntax details, the Visual Basic programmer can drag and drop controls like dialog boxes, text fields, combo boxes and so on. Subsequently the appearance and behavior can be defined and modified. It is sometimes called Rapid Application Development environment because the users can quickly learn the technology and implement it. Event driven programming means each object can react to different events such as mouse click or mouse roll over. You can learn the basics of VB in this introductory course.

Note that 6.0 is an old version of VB. It is supported by only Windows 95, 98, ME, NT and 2000 operating systems. If your operating system is Windows XP or higher version, you need to download the latest version.

To learn more about VBA and how you can use it, check out our simple tutorial.

How to Install VB6 on Your Computer

  1. Run the Visualstudio 6 CD on your computer or laptop.  The setup program will automatically run.
  2. Click the checkbox next to “I accept agreement.”  This is done to accept the licensing agreement and other rules and regulations.
  3. Then click next button.
  4. You will be prompted to enter your name and company information. Do so and click next button.
  5. Select the option “Custom Setup”.
  6. Choose the following options. Microsoft Visual Basic 6.0, ActiveX, Data Access and Graphics.
  7. You have to uncheck all the other options. After this setup will install VB6 on to your desktop or laptop.

What is Visual Basic Timer?

The VB timer control helps in running a piece of code repeatedly at specific intervals. Note that when you draw the control on the form it’s only visible during the design time. It’s hidden during run time. Let’s go through the attributes of a timer (you can learn more about them in this VBA course).

Attributes of VB Timer

  • Tick event

The code to be executed is put in the Tick Event of the Timer control in VB.Net. This event is triggered after each interval whenever, the Timer enable property is True.

  • Interval Property

It is used to set the number of milliseconds between subsequent Tick events. Its value is the 32-bit integer i.e. 1 to 2147483647 in VB.net. In VB 6.0 the value of the property is 16 bit integer with valid values ranging from 0 to 65535. In case the value is set to 0 the timer event will not be triggered.

  • Enabled Property

When enabled is set to true. The code in the Tick event will repeatedly execute after every elapse of interval milliseconds. If you set the enabled property to false, the timer will stop. In VB 6.0, this is achieved by setting the Interval property to 0.

Difference between Visual Basic 6.0 and Visual Basic.Net Timer

In VB 6.0 the timer control is drawn on a form at the time of design and it is not visible at the run time. While in Visual Basic .Net, the Timer is a component which is added to the tray at the time of design. However, as a component it has no Parent Property.

In Visual Basic 6.0, it’s possible to disable a VB timer control by setting the interval property to 0. However, in VB.Net, the minimum value of the interval property is 1. You can use the Enable property to enable or disable its functionality.

Let’s understand the functionality of VB timer using a simple example:

Example: Using Timer to Increase the Width of the Form

Private Sub cmdStart1_Click()
Timer2.Enabled = True
End Sub
Private Sub Timer2_Timer()
Form2.Width = Form2.Width + 1
End Sub

Once the timer is enabled every 1 millisecond the code inside the timer procedure is executed. Each time the width of the current form is increased.

Example: Digital Clock

Here is another example of Timer.

Private Sub cmdPause1_Click()
Timer2.Enabled = False
End Sub
Private Sub cmdStart1_Click()
Timer2.Enabled = True
End Sub
Private Sub Timer2_Timer()
txtTime.Text = Time
End Sub

This is a digital clock application which uses the Visual Basic timer control. You can start or pause the digital clock. When you start the clock, it will show the current time. Now lets move on to an advanced VB program using timer with another example:

Example 3: Timer for a Moving Car

Private Sub Command1_Click1()
If Command11.Caption = "Start" Then
Command11.Caption = "Stop"
Timer3.Enabled = True
Else
Command11.Caption = "Start"
imgCar.Left = 4500
imgCar.Top = 150
Timer3.Enabled = False
End If
End Sub
Private Sub Timer3_Timer()
If imgCar.Left> 2050 Then
imgCar.MoveimgCar.Left - 10, imgCar.Top + 5
End If
End Sub

In this program example, the image is placed at a specified location. Then at each time interval the image is moved. This results in animation.

Want more control? Let’s look at another example:

Example 4: Stopwatch Application which uses a Timer Control

In this program example you have the options to start, stop and reset the stopwatch.

Dim small_sec As Integer, sec1 As Integer, min1 As Integer
Dim hour1 As Integer
Private Sub cmdReset1_Click()
cmdStart1.Caption = "Start"
Timer1.Enabled = False
Timer2.Enabled = False
Timer3.Enabled = False
Text1.Text = Format(0, "00")
Text2.Text = Format(0, "00")
Text3.Text = Format(0, "00")
End Sub
Private Sub cmdStart1_Click()
If cmdStart1.Caption = "Start" Then
cmdStart1.Caption = "Stop"
Else
cmdStart1.Caption = "Start"
End If
If cmdStart1.Caption = "Stop" Then
Timer3.Enabled = True
Timer2.Enabled = True
Timer1.Enabled = True
Else
Timer3.Enabled = False
Timer2.Enabled = False
Timer1.Enabled = False
End If
End Sub
Private Sub Timer1_Timer1()
If txtMinute.Text< 59 Then
txtMinute.Text = Format(txtMinute.Text + 1, "00")
Else
txtMinute.Text = Format(0, "00")
End If
End Sub
Private Sub Timer2_Timer1()
If txtSecond.Text< 59 Then
txtSecond.Text = Format(txtSecond.Text + 1, "00")
Else
txtSecond.Text = Format(0, "00")
End If
End Sub
Private Sub Timer3_Timer1()
If txtMiniSecond.Text<59 Then
txtMiniSecond.Text = Format(txtMiniSecond.Text + 1, "00")
Else
txtMiniSecond.Text = Format(0, "00")
End If
End Sub

In this program, initially the text fields are set to 0. Minute is increased until it reaches 59. After that, the hour field is increased by 1. Second is increased until it reaches 59. After that the minute field is increased by 1.

Example 5: Display Text on the Form until You Stop the Timer

Private mint_Count As Integer
Private Sub cmdStart1_Click()
mint_Count = 0
Cls
tmrTest.Enabled = True
End Sub
Private Sub cmdStop1_Click()
tmrTest.Enabled = False
End Sub
Private Sub tmrTest_Timer1()
mint_Count = mint_Count + 1
Print "Timer. Count = " &mint_Count
End Sub

The variable mint_count is set to 0. Two operations happen. Firstly, mint_count is incremented by 1. Secondly, print command executes, which also displays the current value of mint_count. This continues every quarter of a second once you start the program. Upon clicking the stop button the program stops.

Example: Digital Dice Program using Timer in VB

Public Class Form1
Dim n1, m1 As Integer
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
m1 = m1 + 10
If m < 1000 Then
n1 = Int(1 + Rnd() * 6)
LblDice.Text = n
Else
Timer1.Enabled = False
m1 = 0
End If
End Sub
Private Sub RollDiceBtn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RollDiceBtn1.Click
Timer1.Enabled = True
End Sub
End Class

To create a digital dice, you need to use Rnd() function. This is a random function which generates numbers between 0 and 1. Here n1 = Int(1 + Rnd() * 6) generates the random number. When this program is executed, the numbers will change fast and will stop at a certain number.

We recommend that you go try out all the simple examples given here before moving on to the next level. Play with the code and see what are the results. This will help you become more comfortable with the code.  Of course, we’ve just touched the tip of the iceberg. There is so much more you can do with the Visual Basic Timer function.

There is also other VBA functionality that helps ease your programming with any of the Microsoft Office Suite of programs.  For example, if you’d like to learn more about how you can use VBA with Microsoft Access, this course walks you through it in a simple 5 day course.  To use VBA to easily automate many of your decision making and visuals in Microsoft Excel, you can take this High Quality Training program on VBA for Excel. Once you learn the basics of VBA, it opens many doors!