Visual Basic FAQ

Pure VB: Using a Timer Control as an Alarm
     
Posted:   Thursday December 26, 1996
Updated:   Monday December 26, 2011
     
Applies to:   VB2, VB3, VB4-16, VB4-32, VB5, VB6
     
 Prerequisites
None.

Start a new project, and on the form add a four Option buttons as an array (Option1(1) - Option1(4)), a Timer Control (Timer1) and 2 command buttons (Command1 & Command2). Label Command1 "Test Alarm". Add the following code to the form::
 BAS Module Code
None.

 Form Code
Add a command button to a form along with the following code:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'This holds the amount of time (in milliseconds)
'the Timer will wait before the command button being
'held down is considered an "alarm" call 
Private WaitForPeriod As Integer


Private Sub Command1_MouseDown(Button As Integer, _ 
                               Shift As Integer, _
                               X As Single, Y As Single)
  
  'Start the timer, using the WaitForPeriod chosen 
   Timer1.Interval = WaitForPeriod
   Timer1.Enabled = True
    
End Sub


Private Sub Command1_MouseUp(Button As Integer, _ 
                             Shift As Integer, _
                             X As Single, Y As Single)
    
  'The event, as far as you are concerned,
  'is over, so turn off the timer 
   Timer1.Interval = 0
   Timer1.Enabled = False

End Sub


Private Sub Command2_Click()

    Unload Me

End Sub


Private Sub Form_Load()
  
  'position the form on the screen 
   Me.Move (Screen.Width - Me.Width) / 2, (Screen.Height - Me.Height) / 2

End Sub


Private Sub Option1_Click(Index As Integer)
  
  'use the Index of the control array as the value.
  '1000 is equal to 1 second  
   WaitForPeriod = Index * 1000

End Sub


Private Sub Timer1_Timer()
  
  'The Timer event fires ***only after*** the interval has expired.
  'If the interval is 2000 (2 seconds), then 2 seconds
  'AFTER the command button is pressed, the following
  'MsgBox will be displayed.

  'If the command button is released BEFORE the interval
  'fires this timer, then the MouseUp will turn off the
  'timer and the message box won't display.  
   MsgBox "The alarm interval of" & Str$(WaitForPeriod% \ 1000) & _
           " second(s) has  expired...alarm sent.", _
             48, "Alarm In Progress"
   
  'If it DOES fire, then the msgbox displays, and the
  'Timer = False code assures it won't pop back on screen.
  '(Comment out the next 2 lines and test the code to see
  'what I mean..)  
   Timer1.Interval = 0
   Timer1.Enabled = False
    
End Sub
 Comments

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter