Home

Windows Control: The Timer

 

Introduction to the Timer

 

Introduction

A timer is a non-spatial object that uses recurring lapses of time in a computer or in your application. To work, every lapse of period, the control sends a message to the operating system. The message is something to the effect of "I have counted the number of lapses you asked me to count".

As opposed to the time that controls your computer, a timer is partly but greatly under your control. Users do not see nor do they use a timer as a control. As a programmer, you decide if, why, when, and how to use this control.

 

Creating a Timer

To support timers, the .NET Framework provides the Timer control from the Toolbox and it is implemented through the Timer class. To add it to your application at design time, on the Toolbox, click Timer and click the form. To programmatically add a timer to your application, declare a variable of type Timer and initialize it appropriately. Here is an example:

Imports System.Drawing
Imports System.Windows.Forms

Module Exercise

    Public Class Starter
        Inherits Form

        Private tmr As Timer

        Dim components As System.ComponentModel.Container

        Public Sub New()
            InitializeComponent()
        End Sub

        Public Sub InitializeComponent()
            
            tmr = New Timer

        End Sub

    End Class

    Function Main() As Integer

        Dim frmStart As Starter = New Starter

        Application.Run(frmStart)

        Return 0
    End Function

End Module

Notice that we did not have to add it to the Controls collection of the form.

Practical LearningPractical Learning: Introducing the Timer Control

  1. Start a new Windows Application named ScreenSaver1
  2. Change the following properties of the form:
    BackColor: Black
    FormBorderStyle: None
    WindowState: Maximized
  3. Right-click the form and click View Code
  4. Declare a static integer variable named MoveCounter
     
    Public Class Form1
        Private Shared MoveCounter As Integer
    End Class
  5. In the Class Name combo box, select (Form1 Events)
  6. In the Method Name combo box, select MouseMove and implement the event as follows:
     
    Public Class Form1
        Private Shared MoveCounter As Integer
    
        Private Sub Form1MouseMove(ByVal sender As Object, 
                        ByVal e As System.Windows.Forms.MouseEventArgs) 
                              Handles Me.MouseMove
            Cursor.Hide()
    
            If MoveCounter = 20 Then Close()
            MoveCounter = MoveCounter + 1
        End Sub
    End Class
  7. In the Method Name combo box, select KeyDown and implement the event as follows:
     
    Private Sub Form1KeyDown(ByVal sender As Object, 
                                  ByVal e As System.Windows.Forms.KeyEventArgs) 
                                  Handles Me.KeyDown
            ' If the user presses Esc, stop the screen saver
            If e.KeyCode = Keys.Escape Then Close()
    End Sub
  8. Execute the application to test it (to close the form, you will just move the mouse or press Esc)
  9. Display the form

Characteristics of a Timer

 

The Tick Event

A timer is an object used to count lapses of time and send a message when it has finished counting. Each count is called a tick. When a tick occurs, the control fires a Tick event. This Tick event is of type EventArgs, meaning that it doesn't provide more information than to let you know that a lapse has occurred. Here is an example of implementing it:

Imports System.Drawing
Imports System.Windows.Forms

Module Exercise

    Public Class Starter
        Inherits Form

        Friend WithEvents tmr As Timer

        Dim components As System.ComponentModel.Container

        Public Sub New()
            InitializeComponent()
        End Sub

        Public Sub InitializeComponent()
            
            tmr = New Timer

        End Sub

        Private Sub WasTicked(ByVal sender As Object, 
            ByVal e As EventArgs) Handles tmr.Tick
            Text = CStr(DateTime.Now)
        End Sub
    End Class

    Function Main() As Integer

        Dim frmStart As Starter = New Starter

        Application.Run(frmStart)

        Return 0
    End Function

End Module

The amount of time allocated for counting is called an interval and it is represented by the Interval property. The Interval is a very important characteristic of the timer control because it measures and controls the total time needed to perform a complete count. The Interval is measured in milliseconds. Like any counter, the lower the value, the faster the count will finish, and the higher the value, the longer the count (if you ask one kid to count from 1 to 10 and you ask another to count from 1 to 20 at the same time, if you ask them to shout when they finish, the first kid would finish first and would shout first). The amount of interval you specify will depend on what you are trying to do. Here is an example of setting this property programmatically:

Public Sub InitializeComponent()            
            tmr = New Timer
            tmr.Interval = 200
End Sub

In order for a timer to count, you must tell it when it should start counting. In some applications, you may want the control to work full-time while in some other applications, you may want the control to work only in response to an intermediate event. The ability to stop and start a Timer control can be set using the Enabled Boolean property. Here is an example of setting this property programmatically:

Public Sub InitializeComponent()            
            tmr = New Timer
            tmr.Enabled = True
            tmr.Interval = 200
End Sub

When, or as soon as, this property is set to true, the control starts counting. You can also make it start by calling the Timer.Start() method. Its syntax is:

Public Sub Start

If, when, or as soon as, the Enabled property is set to false, the control stops and resets its counter to 0. You can also stop the timer by calling the Timer.Stop() method. Its syntax is:

Public Sub Stop
 

Practical LearningPractical Learning: Using Timer Controls

  1. From the Components section of the Toolbox, click the Timer control Timer and click the form
  2. In the Properties window, set the Enabled property to True and set the Interval to 200
  3. Under the form, double-click the timer
  4. In the top section of the file, under the other using System lines, type using System.Drawing.Drawing2D;
  5. Scroll down and implement the Tick event as follows:
     
    Private Sub Timer1Tick(ByVal sender As System.Object, 
                                ByVal e As System.EventArgs) 
                                Handles Timer1.Tick
            ' Get the Graphics object of the form
            Dim graph As Graphics = Graphics.FromHwnd(Handle)
    
            ' Generate a random number
            Dim rndNumber As Random = New Random(DateTime.Now.Millisecond)
    
            ' Create a list of the colors of the .NET Framework
            Dim curColor() As Color = 
            { 
                Color.AliceBlue, Color.AntiqueWhite, Color.Aqua, 
                Color.Aquamarine, Color.Azure, Color.Beige, 
                Color.Bisque, Color.Black, Color.BlanchedAlmond, 
                Color.Blue, Color.BlueViolet, Color.Brown, 
                Color.BurlyWood, Color.CadetBlue, Color.Chartreuse, 
                Color.Chocolate, Color.Coral, Color.CornflowerBlue, 
                Color.Cornsilk, Color.Crimson, Color.Cyan, 
                Color.DarkBlue, Color.DarkCyan, Color.DarkGoldenrod, 
                Color.DarkGray, Color.DarkGreen, Color.DarkKhaki, 
                Color.DarkMagenta, Color.DarkOliveGreen, 
                Color.DarkOrange, Color.DarkOrchid, Color.DarkRed, 
                Color.DarkSalmon, Color.DarkSeaGreen, 
                Color.DarkSlateBlue, Color.DarkSlateGray, 
                Color.DarkTurquoise, Color.DarkViolet, Color.DeepPink, 
                Color.DeepSkyBlue, Color.DimGray, Color.DodgerBlue, 
                Color.Firebrick, Color.FloralWhite, Color.ForestGreen, 
                Color.Fuchsia, Color.Gainsboro, Color.GhostWhite, 
                Color.Gold, Color.Goldenrod, Color.Gray, 
                Color.Green, Color.GreenYellow, Color.Honeydew, 
                Color.HotPink, Color.IndianRed, Color.Indigo, 
                Color.Ivory, Color.Khaki, Color.Lavender, 
                Color.LavenderBlush, Color.LawnGreen, Color.LemonChiffon, 
                Color.LightBlue, Color.LightCoral, Color.LightCyan, 
                Color.LightGoldenrodYellow, Color.LightGray, 
                Color.LightGreen, Color.LightPink, Color.LightSalmon, 
                Color.LightSeaGreen, Color.LightSkyBlue, 
                Color.LightSlateGray, Color.LightSteelBlue, 
                Color.LightYellow, Color.Lime, Color.LimeGreen, 
                Color.Linen, Color.Magenta, Color.Maroon, 
                Color.MediumAquamarine, Color.MediumBlue, 
                Color.MediumOrchid, Color.MediumPurple, 
                Color.MediumSeaGreen, Color.MediumSlateBlue, 
                Color.MediumSpringGreen, Color.MediumTurquoise, 
                Color.MediumVioletRed, Color.MidnightBlue, 
                Color.MintCream, Color.MistyRose, Color.Moccasin, 
                Color.NavajoWhite, Color.Navy, Color.OldLace, 
                Color.Olive, Color.OliveDrab, Color.Orange, 
                Color.OrangeRed, Color.Orchid, Color.PaleGoldenrod, 
                Color.PaleGreen, Color.PaleTurquoise, 
                Color.PaleVioletRed, Color.PapayaWhip, 
                Color.PeachPuff, Color.Peru, Color.Pink, Color.Plum, 
                Color.PowderBlue, Color.Purple, Color.Red, 
                Color.RosyBrown, Color.RoyalBlue, Color.SaddleBrown, 
                Color.Salmon, Color.SandyBrown, Color.SeaGreen, 
                Color.SeaShell, Color.Sienna, Color.Silver, 
                Color.SkyBlue, Color.SlateBlue, Color.SlateGray, 
                Color.Snow, Color.SpringGreen, Color.SteelBlue, 
                Color.Tan, Color.Teal, Color.Thistle, Color.Tomato, 
                Color.Transparent, Color.Turquoise, Color.Violet, 
                Color.Wheat, Color.White, Color.WhiteSmoke, 
                Color.Yellow, Color.YellowGreen 
            }
    
            ' Create a list of 10 rectangles for each row
            Dim row1(10) As Rectangle ' = New Rectangle
            Dim row2(10) As Rectangle ' = New Rectangle
            Dim row3(10) As Rectangle ' = New Rectangle
            Dim row4(10) As Rectangle ' = New Rectangle
            Dim row5(10) As Rectangle ' = New Rectangle
            Dim row6(10) As Rectangle ' = New Rectangle
            Dim row7(10) As Rectangle ' = New Rectangle
            Dim row8(10) As Rectangle ' = New Rectangle
    
            ' Create the rectangles that will be drawn on the screen
            For i As Integer = 0 To 9
                row1(i) = New Rectangle(i + (i * (Width / 10)), 0, 
                    (Width - 36) / 10, Height / 8)
    
                row2(i) = New Rectangle(i + (i * (Width / 10)), 
                    4 + (Height / 8), (Width - 36) / 10, Height / 8)
    
                row3(i) = New Rectangle(i + (i * (Width / 10)), 
                    8 + (2 * (Height / 8)), (Width - 36) / 10, Height / 8)
    
                row4(i) = New Rectangle(i + (i * (Width / 10)), 
                    12 + (3 * (Height / 8)), (Width - 36) / 10, Height / 8)
    
                row5(i) = New Rectangle(i + (i * (Width / 10)), 
                    16 + (4 * (Height / 8)), (Width - 36) / 10, Height / 8)
    
                row6(i) = New Rectangle(i + (i * (Width / 10)), 
                    20 + (5 * (Height / 8)), (Width - 36) / 10, Height / 8)
    
                row7(i) = New Rectangle(i + (i * (Width / 10)), 
                    24 + (6 * (Height / 8)), (Width - 36) / 10, Height / 8)
    
                row8(i) = New Rectangle(i + (i * (Width / 10)), 
                    28 + (7 * (Height / 8)), (Width - 36) / 10, Height / 8)
            Next
    
            ' Create the last rectangle of each row
            Dim row1a As Rectangle = New Rectangle(9 + (9 * (Width / 10)), 
                  0, ((Width - 36) / 10) - 2, Height / 8)
            Dim row2a As Rectangle = New Rectangle(9 + (9 * (Width / 10)), 
                  4 + (Height / 8), ((Width - 36) / 10) - 2, Height / 8)
            Dim row3a As Rectangle = New Rectangle(9 + (9 * (Width / 10)), 
                  8 + (2 * (Height / 8)), ((Width - 36) / 10) - 2, Height / 8)
            Dim row4a As Rectangle = New Rectangle(9 + (9 * (Width / 10)), 
                 12 + (3 * (Height / 8)), ((Width - 36) / 10) - 2, Height / 8)
            Dim row5a As Rectangle = New Rectangle(9 + (9 * (Width / 10)), 
                  16 + (4 * (Height / 8)), ((Width - 36) / 10) - 2, Height / 8)
            Dim row6a As Rectangle = New Rectangle(9 + (9 * (Width / 10)), 
                  20 + (5 * (Height / 8)), ((Width - 36) / 10) - 2, Height / 8)
            Dim row7a As Rectangle = New Rectangle(9 + (9 * (Width / 10)), 
                  24 + (6 * (Height / 8)), ((Width - 36) / 10) - 2, Height / 8)
            Dim row8a As Rectangle = New Rectangle(9 + (9 * (Width / 10)), 
                  28 + (7 * (Height / 8)), ((Width - 36) / 10) - 2, Height / 8)
    
            ' Create a list of the hatch brushes 
    	' of the .NET Framework using random colors
            Dim curBrush() As HatchBrush = 
            { 
             New HatchBrush(HatchStyle.BackwardDiagonal, 
                   curColor(rndNumber.Next(curColor.Length)), 
                   curColor(rndNumber.Next(curColor.Length))), 
                New HatchBrush(HatchStyle.Cross, 
           curColor(rndNumber.Next(curColor.Length)), 
           curColor(rndNumber.Next(curColor.Length))), 
             New HatchBrush(HatchStyle.DarkDownwardDiagonal, 
           curColor(rndNumber.Next(curColor.Length)), 
           curColor(rndNumber.Next(curColor.Length))), 
             New HatchBrush(HatchStyle.DarkHorizontal, 
           curColor(rndNumber.Next(curColor.Length)), 
           curColor(rndNumber.Next(curColor.Length))), 
             New HatchBrush(HatchStyle.DarkUpwardDiagonal, 
           curColor(rndNumber.Next(curColor.Length)), 
           curColor(rndNumber.Next(curColor.Length))), 
             New HatchBrush(HatchStyle.DarkVertical, 
           curColor(rndNumber.Next(curColor.Length)), 
           curColor(rndNumber.Next(curColor.Length))), 
             New HatchBrush(HatchStyle.DashedDownwardDiagonal, 
           curColor(rndNumber.Next(curColor.Length)), 
           curColor(rndNumber.Next(curColor.Length))), 
       New HatchBrush(HatchStyle.DashedHorizontal, 
           curColor(rndNumber.Next(curColor.Length)), 
           curColor(rndNumber.Next(curColor.Length))), 
             New HatchBrush(HatchStyle.DashedUpwardDiagonal, 
           curColor(rndNumber.Next(curColor.Length)), 
           curColor(rndNumber.Next(curColor.Length))), 
             New HatchBrush(HatchStyle.DashedVertical, 
           curColor(rndNumber.Next(curColor.Length)), 
           curColor(rndNumber.Next(curColor.Length))), 
             New HatchBrush(HatchStyle.DashedVertical, 
           curColor(rndNumber.Next(curColor.Length)), 
           curColor(rndNumber.Next(curColor.Length))), 
             New HatchBrush(HatchStyle.DiagonalBrick, 
           curColor(rndNumber.Next(curColor.Length)), 
           curColor(rndNumber.Next(curColor.Length))), 
             New HatchBrush(HatchStyle.DiagonalCross, 
           curColor(rndNumber.Next(curColor.Length)), 
           curColor(rndNumber.Next(curColor.Length))), 
             New HatchBrush(HatchStyle.Divot, 
           curColor(rndNumber.Next(curColor.Length)), 
           curColor(rndNumber.Next(curColor.Length))), 
      New HatchBrush(HatchStyle.DottedDiamond, 
       curColor(rndNumber.Next(curColor.Length)), 
       curColor(rndNumber.Next(curColor.Length))), 
     New HatchBrush(HatchStyle.DottedGrid, 
       curColor(rndNumber.Next(curColor.Length)), 
       curColor(rndNumber.Next(curColor.Length))), 
     New HatchBrush(HatchStyle.ForwardDiagonal, 
       curColor(rndNumber.Next(curColor.Length)), 
       curColor(rndNumber.Next(curColor.Length))), 
     New HatchBrush(HatchStyle.Horizontal, 
       curColor(rndNumber.Next(curColor.Length)), 
       curColor(rndNumber.Next(curColor.Length))), 
     New HatchBrush(HatchStyle.HorizontalBrick, 
       curColor(rndNumber.Next(curColor.Length)), 
       curColor(rndNumber.Next(curColor.Length))), 
     New HatchBrush(HatchStyle.LargeCheckerBoard, 
       curColor(rndNumber.Next(curColor.Length)), 
       curColor(rndNumber.Next(curColor.Length))), 
     New HatchBrush(HatchStyle.LargeConfetti, 
       curColor(rndNumber.Next(curColor.Length)), 
       curColor(rndNumber.Next(curColor.Length))), 
     New HatchBrush(HatchStyle.LargeGrid, 
       curColor(rndNumber.Next(curColor.Length)), 
       curColor(rndNumber.Next(curColor.Length))), 
     New HatchBrush(HatchStyle.LightDownwardDiagonal, 
       curColor(rndNumber.Next(curColor.Length)), 
       curColor(rndNumber.Next(curColor.Length))), 
     New HatchBrush(HatchStyle.LightHorizontal, 
       curColor(rndNumber.Next(curColor.Length)), 
       curColor(rndNumber.Next(curColor.Length))), 
     New HatchBrush(HatchStyle.LightUpwardDiagonal, 
       curColor(rndNumber.Next(curColor.Length)), 
       curColor(rndNumber.Next(curColor.Length))), 
     New HatchBrush(HatchStyle.LightVertical, 
       curColor(rndNumber.Next(curColor.Length)), 
       curColor(rndNumber.Next(curColor.Length))), 
     New HatchBrush(HatchStyle.Max, 
       curColor(rndNumber.Next(curColor.Length)), 
       curColor(rndNumber.Next(curColor.Length))), 
     New HatchBrush(HatchStyle.Min, 
       curColor(rndNumber.Next(curColor.Length)), 
       curColor(rndNumber.Next(curColor.Length))), 
     New HatchBrush(HatchStyle.NarrowHorizontal, 
       curColor(rndNumber.Next(curColor.Length)), 
       curColor(rndNumber.Next(curColor.Length))), 
     New HatchBrush(HatchStyle.NarrowVertical, 
       curColor(rndNumber.Next(curColor.Length)), 
       curColor(rndNumber.Next(curColor.Length))), 
     New HatchBrush(HatchStyle.OutlinedDiamond, 
       curColor(rndNumber.Next(curColor.Length)), 
       curColor(rndNumber.Next(curColor.Length))), 
     New HatchBrush(HatchStyle.Percent05, 
       curColor(rndNumber.Next(curColor.Length)), 
       curColor(rndNumber.Next(curColor.Length))), 
     New HatchBrush(HatchStyle.Percent10, 
       curColor(rndNumber.Next(curColor.Length)), 
       curColor(rndNumber.Next(curColor.Length))), 
     New HatchBrush(HatchStyle.Percent20, 
       curColor(rndNumber.Next(curColor.Length)), 
       curColor(rndNumber.Next(curColor.Length))), 
     New HatchBrush(HatchStyle.Percent25, 
       curColor(rndNumber.Next(curColor.Length)), 
       curColor(rndNumber.Next(curColor.Length))), 
     New HatchBrush(HatchStyle.Percent30, 
       curColor(rndNumber.Next(curColor.Length)), 
       curColor(rndNumber.Next(curColor.Length))), 
     New HatchBrush(HatchStyle.Percent40, 
       curColor(rndNumber.Next(curColor.Length)), 
       curColor(rndNumber.Next(curColor.Length))), 
     New HatchBrush(HatchStyle.Percent50, 
       curColor(rndNumber.Next(curColor.Length)), 
       curColor(rndNumber.Next(curColor.Length))), 
     New HatchBrush(HatchStyle.Percent60, 
       curColor(rndNumber.Next(curColor.Length)), 
       curColor(rndNumber.Next(curColor.Length))), 
     New HatchBrush(HatchStyle.Percent70, 
       curColor(rndNumber.Next(curColor.Length)), 
       curColor(rndNumber.Next(curColor.Length))), 
     New HatchBrush(HatchStyle.Percent75, 
       curColor(rndNumber.Next(curColor.Length)), 
       curColor(rndNumber.Next(curColor.Length))), 
     New HatchBrush(HatchStyle.Percent80, 
       curColor(rndNumber.Next(curColor.Length)), 
       curColor(rndNumber.Next(curColor.Length))), 
     New HatchBrush(HatchStyle.Percent90, 
       curColor(rndNumber.Next(curColor.Length)), 
       curColor(rndNumber.Next(curColor.Length))), 
     New HatchBrush(HatchStyle.Plaid, 
       curColor(rndNumber.Next(curColor.Length)), 
       curColor(rndNumber.Next(curColor.Length))), 
     New HatchBrush(HatchStyle.Percent05, 
       curColor(rndNumber.Next(curColor.Length)), 
       curColor(rndNumber.Next(curColor.Length))) 
            }
    
            ' Draw the rectangles to cover the screen
            For i As Integer = 0 To 9
                graph.FillRectangle(curBrush(rndNumber.Next(
    			curBrush.Length)), row1(i))
                graph.DrawRectangle(New Pen(curBrush(rndNumber.Next(
    			curBrush.Length))), row1(i))
    
                graph.FillRectangle(curBrush(rndNumber.Next(
    			curBrush.Length)), row2(i))
                graph.DrawRectangle(New Pen(curBrush(
    			rndNumber.Next(curBrush.Length))), row2(i))
    
                graph.FillRectangle(curBrush(rndNumber.Next(
    			curBrush.Length)), row3(i))
                graph.DrawRectangle(New Pen(curBrush(
    			rndNumber.Next(curBrush.Length))), row3(i))
    
                graph.FillRectangle(curBrush(
    			rndNumber.Next(curBrush.Length)), row4(i))
                graph.DrawRectangle(New Pen(
    			curBrush(rndNumber.Next(curBrush.Length))), row4(i))
    
                graph.FillRectangle(curBrush(
    			rndNumber.Next(curBrush.Length)), row5(i))
                graph.DrawRectangle(New Pen(curBrush(
    			rndNumber.Next(curBrush.Length))), row5(i))
    
                graph.FillRectangle(curBrush(
    			rndNumber.Next(curBrush.Length)), row6(i))
                graph.DrawRectangle(New Pen(curBrush(
    			rndNumber.Next(curBrush.Length))), row6(i))
    
                graph.FillRectangle(curBrush(rndNumber.Next(
    			curBrush.Length)), row7(i))
                graph.DrawRectangle(New Pen(curBrush(
    			rndNumber.Next(curBrush.Length))), row7(i))
    
                graph.FillRectangle(curBrush(rndNumber.Next(
    			curBrush.Length)), row8(i))
                graph.DrawRectangle(New Pen(curBrush(
    			rndNumber.Next(curBrush.Length))), row8(i))
            Next
    
            graph.FillRectangle(curBrush(rndNumber.Next(
    			curBrush.Length)), row1a)
            graph.DrawRectangle(New Pen(curBrush(
    			rndNumber.Next(curBrush.Length))), row1a)
            graph.FillRectangle(curBrush(rndNumber.Next(
    			curBrush.Length)), row2a)
            graph.DrawRectangle(New Pen(curBrush(
    			rndNumber.Next(curBrush.Length))), row2a)
            graph.FillRectangle(curBrush(rndNumber.Next(
    			curBrush.Length)), row3a)
            graph.DrawRectangle(New Pen(curBrush(
    			rndNumber.Next(curBrush.Length))), row3a)
            graph.FillRectangle(curBrush(rndNumber.Next(
    			curBrush.Length)), row4a)
            graph.DrawRectangle(New Pen(curBrush(
    			rndNumber.Next(curBrush.Length))), row4a)
            graph.FillRectangle(curBrush(rndNumber.Next(
    			curBrush.Length)), row5a)
            graph.DrawRectangle(New Pen(curBrush(
    			rndNumber.Next(curBrush.Length))), row5a)
            graph.FillRectangle(curBrush(rndNumber.Next(
    			curBrush.Length)), row6a)
            graph.DrawRectangle(New Pen(curBrush(
    			rndNumber.Next(curBrush.Length))), row6a)
            graph.FillRectangle(curBrush(
    			rndNumber.Next(curBrush.Length)), row7a)
            graph.DrawRectangle(New Pen(curBrush(
    			rndNumber.Next(curBrush.Length))), row7a)
            graph.FillRectangle(curBrush(rndNumber.Next(
    			curBrush.Length)), row8a)
            graph.DrawRectangle(New Pen(curBrush(
    			rndNumber.Next(curBrush.Length))), row8a)
    End Sub		
  6. Execute the application
 

Home Copyright © 2008-2012 FunctionX