Timer Interval property behavior has changed

In Visual Basic 6.0 you could disable a Timer control by setting the Interval property to 0. In Visual Basic 2008, the lower range for the Interval property is 1. If you set the interval to 0, it will throw a run-time exception. The Visual Basic 2008 Timer component uses the Enabled property to allow you to disable or enable it.

The following code illustrates the changes to the Timer control.

' Visual Basic 6.0
Public Function TimerOn(Interval As Integer)
   If Interval > 0 Then
      Timer1.Interval = Interval   ' Enables the timer.
   Else
      Timer1.Interval = 0         ' Disables the timer.
   End If
End Function
' After upgrade to Visual Basic 2008 
Public Function TimerOn(ByRef Interval As Short) As Object
   If Interval > 0 Then
      ' UPGRADE_WARNING: Timer Property Form1.Timer1.Interval has a new
      ' behavior.
      Timer1.Interval = Interval   ' Enables the timer.
   Else
      ' UPGRADE_WARNING: Timer property Timer1.Interval cannot have
      ' a value of 0.
      Timer1.Interval = 0         ' Disables the timer.
   End If
End Function

What to do next

  1. Find any code that sets the Interval property to 0.

  2. Replace it with code that sets the Enabled property to false.

  3. Find any code that sets the Interval property to a value greater than 0.

  4. Add a line of code to set the Enabled property to true.

    Note

    The Enabled property only needs to be set the first time the interval is set. The Timer will remain enabled until it is explicitly disabled by setting the Enabled property to false.

    ' Modified Visual Basic 2008 code
    Public Function TimerOn(ByRef Interval As Short) As Object
       If Interval > 0 Then
          Timer1.Enabled = True      ' Enables the timer.
       Else
          Timer1.Enabled = False      ' Disables the timer.
       End If
    End Function
    

See Also

Concepts

Timer Control for Visual Basic 6.0 Users