Results 1 to 2 of 2

Thread: Timer's

  1. #1
    appi101
    Guest

    Timer's

    Hi

    I want to create a control to replace the standard timer control which comes with VB as its interval is too small. So I built an ActiveX Control and put the relevant settimer and killtimer functions. But to recieve the timer events I must call the Addressof Function and this function only works with functions in a module. But if I move the timerproc function (the function which received notifications of the Timer) to a module then I cannot raise the timer event for the control. So How do i get out of this Catch 22

    Thanks in advance

    Appi
    Attached Files Attached Files

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    You can use the Friend keyword and a reference to the usercontrol, i.e.

    In a standard module in the ActiveX Control Project:
    VB Code:
    1. Private Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
    2. Private Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long
    3.  
    4. Private lTimerHnd As Long
    5.  
    6. ' Store a reference to the Usercontrol
    7. ' so we can call "Friendly" Subs/Functions.
    8. Private oTimerCtrl As ayTimer
    9.  
    10. ' Public Sub to Set the Timer, Requires an Interval in ms and
    11. ' a reference to the calling Usercontrol
    12. Public Sub modSetTimer(ByVal Interval As Long, ByRef oTmr As ayTimer)
    13.     ' Only set the timer if the existing one has been terminated
    14.     If lTimerHnd = 0 Then
    15.         Set oTimerCtrl = oTmr
    16.         lTimerHnd = SetTimer(0, 1, Interval, AddressOf TimerProc)
    17.     End If
    18. End Sub
    19.  
    20. Public Sub modStopTimer()
    21.     ' Stop the existing Timer
    22.     If lTimerHnd Then
    23.         Call KillTimer(0, lTimerHnd)
    24.         lTimerHnd = 0
    25.         Set oTimerCtrl = Nothing
    26.     End If
    27. End Sub
    28.  
    29. Private Sub TimerProc(ByVal hWnd As Long, ByVal Msg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
    30.     ' Raise the "Friend" Sub in the Usercontrol which in turn
    31.     ' Raises the TimerFire Event for the Control
    32.     oTimerCtrl.RaiseTimerEvent
    33. End Sub
    In the User Control (I've named it ayTimer):
    VB Code:
    1. Private bFiring As Boolean
    2. Private bEnabled As Boolean
    3. Private lInterval As Long
    4.  
    5. ' Timer Event
    6. Event TimerFire()
    7.  
    8. ' Friendly Sub used to Raise the Event from the standard module
    9. Friend Sub RaiseTimerEvent()
    10.     RaiseEvent TimerFire
    11. End Sub
    12.  
    13. ' Retreive the Timer Control Properties
    14. Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
    15.     bEnabled = PropBag.ReadProperty("Enabled", True)
    16.     lInterval = PropBag.ReadProperty("Interval", 0)
    17.     StartTimer
    18. End Sub
    19.  
    20. ' Store the Timer Control Properties
    21. Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
    22.     Call PropBag.WriteProperty("Enabled", bEnabled)
    23.     Call PropBag.WriteProperty("Interval", lInterval)
    24. End Sub
    25.  
    26. ' Stop the Timer if it's going when the control is destroyed
    27. Private Sub UserControl_Terminate()
    28.     If bFiring Then
    29.         modStopTimer
    30.         bFiring = False
    31.     End If
    32. End Sub
    33.  
    34. ' Return the Timer Interval
    35. Public Property Get Interval() As Long
    36.     Interval = lInterval
    37. End Property
    38.  
    39. ' Set the Timer Interval
    40. Public Property Let Interval(ByVal vNewValue As Long)
    41.     lInterval = vNewValue
    42.     If lInterval = 0 Then
    43.         StopTimer
    44.     Else
    45.         StartTimer
    46.     End If
    47. End Property
    48.  
    49. ' Return the Timers "Enabled" status
    50. Public Property Get Enabled() As Boolean
    51.     Enabled = bEnabled
    52. End Property
    53.  
    54. ' Set the Timers "Enabled" status
    55. Public Property Let Enabled(ByVal vNewValue As Boolean)
    56.     bEnabled = vNewValue
    57.     If Not bEnabled Then
    58.         StopTimer
    59.     Else
    60.         StartTimer
    61.     End If
    62. End Property
    63.  
    64. ' Start the Timer (only at runtime)
    65. Private Sub StartTimer()
    66.     If Not UserControl.Ambient.UserMode Then Exit Sub
    67.     If bEnabled And lInterval > 0 Then
    68.         StopTimer
    69.         Call modSetTimer(lInterval, Me)
    70.         bFiring = True
    71.     End If
    72. End Sub
    73.  
    74. ' Stop the Timer (only at runtime)
    75. Private Sub StopTimer()
    76.     If Not UserControl.Ambient.UserMode Then Exit Sub
    77.     If bFiring Then
    78.         Call modStopTimer
    79.     End If
    80.     bFiring = False
    81. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width