|
-
Feb 15th, 2007, 03:29 PM
#1
Thread Starter
Addicted Member
[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...
-
Feb 15th, 2007, 06:32 PM
#2
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.
-
Feb 15th, 2007, 06:55 PM
#3
Thread Starter
Addicted Member
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.
-
Feb 16th, 2007, 02:41 AM
#4
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:
'Collection class
Private mCol As Collection
Event DoSomething(Instance As Class1)
Public Sub RaiseTheEvent(Instance As Class1)
RaiseEvent DoSomething(Instance)
End Sub
Public Function Add(Optional sKey As String) As Class1
Dim objNewMember As Class1
Set objNewMember = New Class1
If Len(sKey) = 0 Then
mCol.Add objNewMember
Else
mCol.Add objNewMember, sKey
End If
Set objNewMember.Parent = Me 'class needs a reference
Set Add = objNewMember
Set objNewMember = Nothing
End Function
'Class Module
Public WithEvents DLLInstance As SomeDLL.SomeClass
Public MyParent As Collection1
Public SomeProperty as String
Private Sub DLLInstance_SomeEvent()
MyParent.RaiseTheEvent Me 'call the Collection method
End Sub
'Form
Private WithEvents colInstances As Collection1
Private Sub Form_Load()
Set colInstances = New Collection1
End Sub
Private Sub SetNodeInstance(Node as Node)
Set Node.Tag = colInstances.Add
End Sub
Private Sub colInstances_DoSomething(Instance As Instance)
Debug.Print Instance.SomeProperty, Instance.DLLInstance.SomeProperty
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.
-
Feb 16th, 2007, 02:51 AM
#5
Re: Question for advanced users
Just for reference, More on WithEvents
-
Feb 19th, 2007, 07:08 AM
#6
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|