Results 1 to 9 of 9

Thread: [RESOLVED] Raise event in a Class module through a module (Thanks to Merri).

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    Egypt
    Posts
    162

    Resolved [RESOLVED] Raise event in a Class module through a module (Thanks to Merri).

    Hi everybody , currently I want to make a keyboard hook but as an ActiveX Dll , I use Joacim Andersson's method for doing this , like here it work perfectly but it use AddressOf so I must use a module , my question is : If I have event in my class module say [ Public Event KeyPressed (VirutalKey As Long) ] how can I raise it through the module ?

    I know that there is a way in the API Timer made by Wokawidget but I don't know how to use it in my case , can anyone help ?!

    Btw : I made a search but nothing relevant if it was discussed be4 I want links please.

    Thanks & Have a nice day
    Last edited by msayed2004; May 15th, 2007 at 05:41 PM.
    Mohammed Sayed - Egypt - Anesthetist



    =

    =

  2. #2
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    Egypt
    Posts
    162

    Exclamation 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:
    1. Option Explicit
    2. Public Event DestroyMyPC()
    3.  
    4. Friend Sub RaiseMyEvent()
    5.     RaiseEvent DestroyMyPC
    6. End Sub
    7.  
    8. Private Sub Class_Initialize()
    9. KbdCollection.Add Me, CStr(ObjPtr(Me))
    10. Goo
    11. End Sub
    12.  
    13. Private Sub Class_Terminate()
    14. KbdCollection.Remove CStr(ObjPtr(Me))
    15. End Sub

    And in a module :
    vb Code:
    1. Option Explicit
    2. Public KbdCollection As New Collection
    3.  
    4. Public Sub Goo()
    5.     'What should be here
    6. End Sub

    And in the form :
    vb Code:
    1. Option Explicit
    2. Private WithEvents MyClass As Class1
    3.  
    4. Private Sub Form_Load()
    5. Set MyClass = New Class1
    6. End Sub
    7.  
    8. Private Sub MyClass_DestroyMyPC()
    9. MsgBox "Done"
    10. End Sub

    What should I do in the Goo Sub to call the RaiseMyEvent Sub

    Thanks
    Mohammed Sayed - Egypt - Anesthetist



    =

    =

  4. #4
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    Egypt
    Posts
    162

    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.
    Mohammed Sayed - Egypt - Anesthetist



    =

    =

  6. #6
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    Egypt
    Posts
    162

    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.
    Mohammed Sayed - Egypt - Anesthetist



    =

    =

  8. #8
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Location
    Egypt
    Posts
    162

    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.
    Mohammed Sayed - Egypt - Anesthetist



    =

    =

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