|
-
May 14th, 2007, 07:39 AM
#1
Thread Starter
Addicted Member
-
May 14th, 2007, 07:56 AM
#2
Re: Raise event in a Class module through a module.
One thing you could do is to make a public Collection where each class automatically adds itself and removes as well once done. Like:
Code:
' in the module
Public KbdCollection As New Collection
' in Initialize of a Class Module
KbdCollection.Add Me, CStr(ObjPtr(Me))
' in Terminate of a Class Module
KbdCollection.Remove CStr(ObjPtr(Me))
Now as you know all the classes that exist, you can easily call them so that events are raised.
As a notice, I've never actually tried if it is possible to do this. Hopefully it is.
-
May 14th, 2007, 08:40 AM
#3
Thread Starter
Addicted Member
Re: Raise event in a Class module through a module.
OK , Thanks for reply but I don't know how to finish this.
Say I made this :
In a Class Module (Class1) :
vb Code:
Option Explicit
Public Event DestroyMyPC()
Friend Sub RaiseMyEvent()
RaiseEvent DestroyMyPC
End Sub
Private Sub Class_Initialize()
KbdCollection.Add Me, CStr(ObjPtr(Me))
Goo
End Sub
Private Sub Class_Terminate()
KbdCollection.Remove CStr(ObjPtr(Me))
End Sub
And in a module :
vb Code:
Option Explicit
Public KbdCollection As New Collection
Public Sub Goo()
'What should be here
End Sub
And in the form :
vb Code:
Option Explicit
Private WithEvents MyClass As Class1
Private Sub Form_Load()
Set MyClass = New Class1
End Sub
Private Sub MyClass_DestroyMyPC()
MsgBox "Done"
End Sub
What should I do in the Goo Sub to call the RaiseMyEvent Sub
Thanks
-
May 14th, 2007, 08:52 PM
#4
Re: Raise event in a Class module through a module.
You can't call Goo on Initialize, because class isn't still fully ready at that point and it won't raise events. I moved Goo into a command button and it started to work.
Code:
' Form1.frm
Option Explicit
Private WithEvents MyClass As Class1
Private Sub Command1_Click()
Goo
End Sub
Private Sub Form_Load()
Set MyClass = New Class1
End Sub
Private Sub MyClass_DestroyMyPC()
Debug.Print "Goo!"
End Sub
Code:
' Module1.bas
Option Explicit
Public KbdCollection As New Collection
Public Sub Goo()
Dim Kbd As Class1
For Each Kbd In KbdCollection
Kbd.RaiseMyEvent
Next Kbd
End Sub
Code:
' Class1.cls
Option Explicit
Public Event DestroyMyPC()
Friend Sub RaiseMyEvent()
RaiseEvent DestroyMyPC
End Sub
Private Sub Class_Initialize()
KbdCollection.Add Me, CStr(ObjPtr(Me))
End Sub
Private Sub Class_Terminate()
KbdCollection.Remove CStr(ObjPtr(Me))
End Sub
-
May 15th, 2007, 04:01 AM
#5
Thread Starter
Addicted Member
Re: Raise event in a Class module through a module.
Thanks but can I call it from the class later (by a public sub) ?
Thanks for your concern 
Edit : About to say resolved , but I will try to complete the DLL fist before marking it as Resolved , Thanks Merri
Last edited by msayed2004; May 15th, 2007 at 06:25 AM.
-
May 15th, 2007, 07:59 AM
#6
Re: Raise event in a Class module through a module.
Yes, you can call the procedure from the class, just don't do it in Initialize
-
May 15th, 2007, 05:35 PM
#7
Thread Starter
Addicted Member
Re: Raise event in a Class module through a module.
Well thanks it is Resolved now , I made it ............. Oooops sorry Merri made it 
Just want to know in addition to your name , your site & vbForums what should I add to the credits.
Thanks again 
Edit : One more related question : should I use Virtual Keys list (that one @ MSDN) to convert the results to letters (characters) or there is a way to do this directly by any function (built in or API) ,Thanks.
Last edited by msayed2004; May 15th, 2007 at 05:45 PM.
-
May 16th, 2007, 10:22 AM
#8
Re: [RESOLVED] Raise event in a Class module through a module (Thanks to Merri).
This was so minor that I don't know if it is even worth crediting. My help was what, three to five lines of code depending on how compact you want to write it 
As for your new question, see this solution at vbAccelerator.
-
May 16th, 2007, 10:29 AM
#9
Thread Starter
Addicted Member
Re: [RESOLVED] Raise event in a Class module through a module (Thanks to Merri).
Thanks again , I will take a look.
Last edited by msayed2004; May 16th, 2007 at 10:33 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|