Reputation: 462
I need to construct a VB6.0 ocx that will be used as a plugin for some external VB6.0 applications
This ocx contains several sub procedures that are supposed to work as event-handlers for some of the external events (External to ocx).
The problem is, the users who use this ocx will only call one of my ocx sub procedures once and only once. Question is , how do i bind all my sub-proc/functions to their respective external events upon this one time initialization so that my procedures will get called when their events fire?
I managed to do this within the external app itself, but not when i ported these codes to OCX and use it as plugin in an external vb program
Let say this is the original event handler in the external vb app:
Private Sub someExternalControl1_someEvent(someParameter as boolean)
MsgBox ("The original event handler")
End Sub
and in that vb app if i do:
dim withevents aaa as someExternalControl
set aaa = someExternalControl1
then this custom event handler will also be called everytime the event fires along with the original event handler as stated above
Private Sub aaa_someEvent(someParameter as boolean)
MsgBox ("The custom event handler")
End Sub
but i couldn't make the same when i put this in OCX. Because i couldn't do:
Public WithEvents ocxMyPlugin As VBControlExtender
...
Set ocxMyPlugin = Controls.Add("myprogID.usercontrolname", "somename", Me)
Set ocxMyPlugin.object.someExternalControl2 = someExternalControl ' this will raise error
Upvotes: 2
Views: 3881
Reputation: 7196
While I never handled this situation before I remembered something about a VBControlExtender object that is used when you dynamically add controls to a form.
Poking around I found this article. Then this MSDN documention on VBControlExtender.
In particular you want to look at ObjectEvent.
Understand there is no good way to dynamically assign methods to events like in .NET. VB6 handles events by dimensioning a variable with the WithEvents Keyword.
However it is a variable. So while you can't change the method you can change the object the variable points too.
If you have
Dim WithEvents X as SomeControl
Dim Y as New SomeControl
Dim Z as New SomeControl
Private Sub X_MyEvent(ByVal MyParm as Variant)
'Do Something like display the control name
End if
Public Sub TestY
Set X = Y
End Sub
Public Sub TestZ
Set X = Z
End Sub
If you activate the event after TestY then X_MyEvent will be handling the events for control Y, If you active the event after TestZ then the X_MyEvent will be handling the events for control Z.
With VBControlExtender you can handle different controls generically. If you instantiating multiple controls of the same type then you have quite a bit of work to do. You can't use withevent with arrays. In that case I would create a class with events to help me handle multiple controls of the same type.
Upvotes: 1
Reputation: 14603
I might need more details to help. What is the type of ocxMyPlugin.object.someExternalControl2. If its unknown to the target application, you will have problems about late binding. However, you may create a wrapper or an interface on a third (or fourth project) which may solve your problem. Its important to post your design and what exactly you are trying to achieve. May be there are other methods which can be used instead.
Upvotes: 0