Results 1 to 6 of 6

Thread: [RESOLVED] Question for advanced users

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Posts
    160

    Resolved [RESOLVED] Question for advanced users

    Short story... I've "attached" some objects (class modules) to different types of nodes in a TreeView. One of these class modules has set in it a reference to a DLL that controls a part of an external device.

    The problem is that this reference raise events that I need to catch in my main MDI Form, and then access to the object set in the main node of the corresponding branch, get some data from it and launch some functions.

    Of course, the number of main nodes (and therefore children) isn't fixed, so I can never know how many of these objects are.

    Any tip? maybe this could be done in a simple way, but I've got so many things to do that I'm out of ideas right now...

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Question for advanced users

    Are you familiar with WithEvents? Declaring an object like

    Private WithEvents oMyClass as MyClass

    will allow you to handle any events raised by that object.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Posts
    160

    Re: Question for advanced users

    As I say, the number of classes set is not fixed, so if I wanted to make an instance for all of them I'd have to make an array, and, AFAIK, that's not possible for objects with events.

    Also, it's not the class the one raising the events (which I should create first if I'd want to go by that route, and then, I'd have the problem I've stated above if I'm not wrong), but an object inside it.

  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Question for advanced users

    True you cannot use an array but you can use a Collection. It is a pain but doable.

    Create a class that contains the instance of the DLL Class (using WithEvents). The DLL class will then raise its events into your class. Your class then calls a procedure in the Collection. This procedure then raises the Collection Events to the Form.

    You can have as many events as needed passing any number of arguments.

    VB Code:
    1. 'Collection class
    2. Private mCol As Collection
    3.  
    4. Event DoSomething(Instance As Class1)
    5.  
    6. Public Sub RaiseTheEvent(Instance As Class1)
    7.     RaiseEvent DoSomething(Instance)
    8. End Sub
    9.  
    10. Public Function Add(Optional sKey As String) As Class1
    11.     Dim objNewMember As Class1
    12.     Set objNewMember = New Class1
    13.  
    14.     If Len(sKey) = 0 Then
    15.         mCol.Add objNewMember
    16.     Else
    17.         mCol.Add objNewMember, sKey
    18.     End If
    19.    
    20.     Set objNewMember.Parent = Me 'class needs a reference
    21.  
    22.     Set Add = objNewMember
    23.     Set objNewMember = Nothing
    24. End Function
    25.  
    26. 'Class Module
    27. Public WithEvents DLLInstance As SomeDLL.SomeClass
    28. Public MyParent As Collection1
    29. Public SomeProperty as String
    30.  
    31. Private Sub DLLInstance_SomeEvent()
    32.     MyParent.RaiseTheEvent Me 'call the Collection method
    33. End Sub
    34.  
    35. 'Form
    36. Private WithEvents colInstances As Collection1
    37.  
    38. Private Sub Form_Load()
    39.    Set colInstances = New Collection1
    40. End Sub
    41.  
    42. Private Sub SetNodeInstance(Node as Node)
    43.     Set Node.Tag = colInstances.Add
    44. End Sub
    45.  
    46. Private Sub colInstances_DoSomething(Instance As Instance)
    47.     Debug.Print Instance.SomeProperty, Instance.DLLInstance.SomeProperty
    48. End Sub

    This does create a circular reference (Collection1 references Class1. Class1 references Collection1 via the Parent Property). Circular references are normally discourage but I have used a similar situation without problems. Just make sure you set the Class.Parent property to nothing before the releasing the Collection.

  5. #5
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: Question for advanced users

    Just for reference, More on WithEvents
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Posts
    160

    Re: Question for advanced users

    Thanks for the reply brucevde, it worked flawlessly after a few more customizations.

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