PDA

Click to See Complete Forum and Search --> : Problem with "WithEvents"...


[Digital-X-Treme]
Jul 14th, 2001, 09:55 AM
I have the following 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


'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

peet
Jul 14th, 2001, 10:15 AM
Private Sub Class_Initialize()
Set mTimer = New Timer
mTimer.Enabled = True
Debug.Print "Initialized..."
End Sub


maybe this works ?

[Digital-X-Treme]
Jul 14th, 2001, 02:26 PM
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?

Tryster
Jul 14th, 2001, 02:57 PM
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 (http://www.allapi.net)

Cheers

Tryster