Results 1 to 4 of 4

Thread: Problem with "WithEvents"...

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2000
    Location
    UK.
    Posts
    728

    Problem with "WithEvents"...

    I have the following code:

    Code:
    'CTimer.cls
    Option Explicit
    
    Event Timer()
    Private WithEvents mTimer As Timer
    Private intCount As Integer
    
    Public Property Let Interval(ByVal intInterval As Integer)
    mTimer.Interval = intInterval
    End Property
    Public Property Get Interval() As Integer
    Interval = intInterval
    End Property
    
    Private Sub Class_Initialize()
        mTimer.Enabled = True
        Debug.Print "Initialized..."
    End Sub
    
    Private Sub mTimer_Timer()
        intCount = intCount + 1
        RaiseEvent Timer
    End Sub
    
    Public Sub Count()
    Debug.Print intCount
    End Sub
    Code:
    'Form1
    Option Explicit
    Private WithEvents mTimerObj As CTimer
    
    Private Sub Form_Load()
    Set mTimerObj = New CTimer
    End Sub
    
    Private Sub mTimerObj_Timer()
    Debug.Print "Timer Event!"
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
    Set mTimerObj = Nothing
    End Sub
    Now, what im trying to do is basically encapsulate the timer control inside a class, with a few extra properties.

    When i run the above code, i get an error when the Class_Initialize() sub runs, saying "Object variable or With Block variable Not Set". Any ideas?

    I want to do this without building a separate OCX. I have never really looked into ActivX before. Can anyone point me in the right direction?

    Thanks in advance

    Laterz
    Digital-X-Treme
    Contact me on MSN Messenger: [email protected]

    [VBCODE]Debug.Print Round(((1097) - ((55 ^ 5 + 311 ^ 3 - 11 ^ 3) _
    / (68 ^ 5))) ^ (1 / 7), 13)[/VBCODE]

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Code:
    Private Sub Class_Initialize()
        Set mTimer = New Timer
        mTimer.Enabled = True
        Debug.Print "Initialized..."
    End Sub
    maybe this works ?
    -= a peet post =-

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2000
    Location
    UK.
    Posts
    728

    Unhappy Still not working...

    peet, thanks for the reply. I tried the code you provided but i still get a compile error: "Invalid use of "NEW" keyword"...

    Anyone got any ideas?
    Digital-X-Treme
    Contact me on MSN Messenger: [email protected]

    [VBCODE]Debug.Print Round(((1097) - ((55 ^ 5 + 311 ^ 3 - 11 ^ 3) _
    / (68 ^ 5))) ^ (1 / 7), 13)[/VBCODE]

  4. #4
    Member
    Join Date
    Dec 2000
    Location
    UK
    Posts
    39
    Controls can not normally be created in this way, because they do not implement the IDispatch interface. You could either include a form in the ActiveX component and place the timer on this, or (more preferably) use the Settimer & KillTimer API calls.

    For info on SetTimer & KillTimer see KPD

    Cheers

    Tryster

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