The Bare Basics of the Timer Control

Introduction

We've all come across the need for a timer in one or more of the applications that we write. In this article, you are going to look at some of the basics of timers, as well as look at a few examples of how to use a timer effectively.

The first problem that I've noticed in the years that I've been assisting beginners on the forum, is that many think of the Timer Control as a Stopwatch timer, or a count down timer. THIS IT IS NOT; the timer is a control that simply raises an event every X milliseconds.

So then, how does it work? Well, at the end of this article there is a VB6 downloadable project that has several examples of how a timer can be used. You will look at these examples to get a better understanding of how timers work.

But first, what does the timer actually do? It's a control that has no display during run time; it does not count or calculate. It will raise an event only every X ms where you as the programmer must now do the displaying, counting, and/or calculating.

The timer has very few properties, and only two of them are of any interest when programming, Interval and Enabled.

Enabled simply switches the timer on and off.

Interval is the time in ms between events, with a max time of 65535ms, or 6.5 seconds. Also something to note, the Timer control is linked into a hardware timer that is raised every 16ms and therefore is accurate only up to every 16th ms.

The first example you will look at is how to use a timer to keep a time display updated.

Update on Screen Time

Object

To display the current date and time in a label on your form, that is regularly updated and no more than one second out.

Method

You add a label to your form, and name it Time1; you also add a timer to the form. The timer control can be placed anywhere; however, I find it more convenient to place it just off the bottom or side so that visually it's out the way.

In your form load event, you initiate the timer so that it shows the current time from the moment your application starts. You enable the timer and set the interval for the timer to 500ms. The interval could be set at every 1000ms (1 second); however, this raises the small issue of accuracy, although the displayed time will never be more than 1 second out. There are some circumstances where the timer will not occur exactly one second later, and the display will tend to skip a second; although not critical, for myself personally this is a visual pain.

Listing 1 shows the key code required for the timer to operate correctly.

Listing 1

[Form_Load]
Timer1.Interval = 500
Timer1.Enabled = True

[Timer1_Timer]
Time1.Caption = "Date: " & Date & " - Time: " & Time

When the application is now run, the time will be constantly updated on your form, the CPU usage is minimal, and any other functions on the form will not be affected.

Moving Objects

In the second example, you will look at using a timer to move items/pictures/animation around using key presses.

Note: The H key is used for left movement, K is right, U is up, and M is down.

Object

To move a picture around inside a picturebox using the keyboard as the move triggers (W A S D X).

Method

For this, you use a picturebox inside a picturebox. You also set the form's Keypreview to True so that you can have a single function to handle the key presses.

You place your first picturebox so that it almost fills the form and set its Scalemode to '3-Pixel'. Then, you place the second, smaller picturebox inside the first. You also add a timer to the form, again just off the edge of the form.

Now, the coding gets a little interesting here. You create a tristate type, similar to a Boolean type, but you now have three possibilities: Forward, Stop, and Backwards. Or rather, you use positive (+1), zero (0), and negative (-1) so that you have a direction multiplier.

You create two variables with your tristate type, namely P1UD for the up/down movement of the picture and P1LR for the left/right movement.

You now use the form's keydown event to capture the key presses. And, depending on the keys pressed, you set your tristate variables according to the following list.

  • W: P1UD = Negative (up)
  • A: P1LR = Negative (left)
  • D: P1LR = Positive (Right)
  • X: P1UD = Positive (Down)
  • S: Both P1UD and P1LR = Zero.

With this setup, you now can tap any of the above letters and the appropriate variable will bet set for forward or backward motion. Pressing 'S', the center key, will set both your variables to stop the motion.

This setup will allow constant motion, with the picture moving until the 'S' stop key is pressed. You also can set up that, when a key is released, the motion stops. For this, you use the form's keyup event to set your variables to zero according to the following list.

  • W, X: P1UD = Zero
  • A, D: P1LR = Zero

With all the theory behind you, look at the code. Listing 2 shows all the key bits of code and declarations required to make the picturebox move around.

Listing 2

[General]
Private Enum Tristate
   Neg  = -1
   Zero = 0
   Pos  = 1
End Enum
Private P1UD As Tristate
Private P1LR As Tristate
Private AutoStop As Boolean

[Form_Load]
Picture2.Picture = LoadPicture(App.Path & "/pic1.bmp")
Timer1.Interval  = 50
Timer1.Enabled   = True
SPM = 1    'steps per timed tick

[Form_KeyDown]
Select Case KeyCode
   Case 65
      P1LR = neg
   Case 68
      P1LR = pos
   Case 87
      P1UD = neg
   Case 88
      P1UD = pos
   Case 83
      P1LR = zero
      P1UD = zero
End Select

[Form_KeyUp]
If AutoStop Then
   Select Case KeyCode
      Case 65, 68
         P1LR = zero
      Case 87, 88
         P1UD = zero
   End Select
End If

[Timer1_Timer]
Picture2.Top = Picture2.Top + SPM * P1UD
   If Picture2.Top <= 0 Then P1UD = pos
   If Picture2.Top >= Picture1.ScaleHeight Then P1UD = neg
Picture2.Left = Picture2.Left + SPM * P1LR
   If Picture2.Left <= 0 Then P1LR = pos
   If Picture2.Left >= Picture1.ScaleWidth Then P1LR = neg

You also will notice that in the timer event I'm checking for the borders and changing the direction of movement to keep the picture within the borders.

The variable SPM is used to set the steps per movement, or how many pixels the picture will move with each timer tick.

Again, using a timer in this method reduces the CPU usage and gives a smoother movement without affecting too much of the rest of the application's events.

The Bare Basics of the Timer Control

Animation

In the last example, you look at doing a little animation with a timer.

Object

To animate a selection box, or rather cause it to flash.

Method

Have you ever noticed the selection box animation in most picture editing software, and wondered how it's done? Well, this short example will give you all you need to duplicate this animation.

It's a bit more than just simple animation. You have to keep record of the position of the selection box, the current animation status, and several other items.

In this example, I'm not going to cover the code for drawing and setting the selection box but more on how to animate it once it's drawn.

In your project, you add a picturebox to your form; set it to fill almost the entire form. You add the mouse up and down events to create your selection box (the code is in the download project). You add a timer to the form.

You now have a variable that stores the selection box position and size, set via the mouse click events. This variable holds the top, left, width, and height of your animated selection box.

Once your selection box has been defined, you then init the timer and let it run the animation. On any mouse click, you stop the timer, and hence the animation, and return the selection box to its original state.

Listing 3

[General]
Private Type RECT
   Left As Long
   Top As Long
   Width As Long
   Height As Long
End Type
Private SelRect As RECT
Private SelRect_S As Boolean

[Picture1_MouseDown]
   If Button = vbLeftButton Then
      Timer1.Enabled = False
      If SelRect_S Then
         Picture1.DrawStyle = vbSolid
         Picture1.Line (SelRect.Left, SelRect.Top)- _
            (SelRect.Left + SelRect.Width, SelRect.Top + _
             SelRect.Height), , B
         Picture1.DrawStyle = SelDrawType
      End If
   End If

[Picture1_MouseUp]
   If Button = vbLeftButton Then
      Timer1.Enabled = True
      Timer1.Interval = 10
   End If

[Timer1_Timer]
SelRect_S = Not (SelRect_S)
Picture1.DrawStyle = vbSolid
Picture1.DrawMode = 6 'inverted
Picture1.Line (SelRect.Left, SelRect.Top)-(SelRect.Left + _
               SelRect.Width, SelRect.Top + SelRect.Height), , B
Picture1.DrawStyle = SelDrawType

The animation is simply the effect of inverting the selection box at a regular interval. In the download, the selection box is a dashed line that in the animation has an excellent visual effect.

Conclusion

At first glance, timers might seem like they have no real use. Because of the first misconception that they are stopwatch timers, beginners try to use them as such. After realizing that a timer does not work as a stopwatch but requires much more to provide this function, many beginners simply put timers aside as a useless control, and use longer, looping orientated functions that slave the CPU and the application.

Once you have a full understanding of timers and the examples listed here, you can see how effective the use of timers is, both on the application and the CPU usage.

In the download, there are five timer functions used all at different intervals.

  1. To display the time.
  2. To move the pictures
  3. To animate the selection box.
  4. To play a snip of stick men animation.
  5. A count down timer.

I hope these few examples lead to better timer usage.



About the Author

Richard Newcombe

Richard Newcombe has been involved in computers since the time of the Commodore 64. Today, he has excelled in programming, and designs. Richard is in his mid 30's and, if or when you looking for him look no further than his computer. Always willing to help and give advice where he can in regard to computer related subjects. At present he is working as a .NET 2008 Software Developer for Syncrony Web Services, South Africa.

Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Stories about devastating cyberattacks are plaguing the news. Why? The DNS protocol is easy to exploit. See your network the way hackers do—as an easy target. Learn how you can effectively secure your DNS infrastructure today.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds