Re: How do I use WithEvents?
Can anyone help me with this?
I experimented a little and found that doing WithEvents with an Object doesn't work because you have to specify a specific control. Is there any way to get access to the events of an object without knowing what object it will be, but knowing it will have a Resize event?
Re: How do I use WithEvents?
The events are part of the class's interface, so you can't declare a generic Object WithEvents. What you can do, I think, is declare an instance interface class WithEvents and make all your classes Implement that interface. Pop your events in there and it should work (assuming VB6 handles interfaces the way I think).
Re: How do I use WithEvents?
You can create a generic event handler for ActiveX controls using the VBControlExtender object.
VB Code:
Private WithEvents oControlEvents As VBControlExtender
Private Sub Form_Load()
Set oControlEvents = Me.MSFlexGrid1
End Sub
Private Sub oControlEvents_ObjectEvent(Info As EventInfo)
Dim oParam As EventParameter
Debug.Print Info.Name;
For Each oParam In Info.EventParameters
Debug.Print , oParam.Name, oParam.Value
Next
End Sub
Note that VB intrinsic controls do not implement the ControlExtender.
1 Attachment(s)
Re: How do I use WithEvents?
Attached is a simple example of using Withevents from your own classes.
Hope this helps!!
:wave: :thumb: :wave:
Re: How do I use WithEvents?
Quote:
Originally Posted by penagate
The events are part of the class's interface, so you can't declare a generic Object WithEvents. What you can do, I think, is declare an instance interface class WithEvents and make all your classes Implement that interface. Pop your events in there and it should work (assuming VB6 handles interfaces the way I think).
What is an instance interface class?
Re: How do I use WithEvents?
Quote:
Originally Posted by brucevde
You can create a generic event handler for ActiveX controls using the VBControlExtender object.
Note that VB intrinsic controls do not implement the ControlExtender.
By intrinsic control, do you mean the standard ones that are always available?
If thats the case then I am stuck again. Basically I need to be able to get the events of a PictureBox OR a custom control.
I couldn't even subclass if I wanted to because it is likely that the controls I will be dealing with don't have .hWnds.
Do I have any other options?
Re: How do I use WithEvents?
Quote:
Originally Posted by penagate
The events are part of the class's interface, so you can't declare a generic Object WithEvents. What you can do, I think, is declare an instance interface class WithEvents and make all your classes Implement that interface. Pop your events in there and it should work (assuming VB6 handles interfaces the way I think).
As far as I understood the whole event stuff, interfaces which support events
only implement IConnectionPointContainer, but the events have their own interface.
So what you do if you want events:
get IConnectionPointContainer from the interface,
take the first IConnectionPoint returned by IConnectionPointContainer.EnumConnectionPoints,
create a new interface which implements IDispatch (event sink),
advise your event sink to IConnectionPoint,
and capture events through IDispatch.Invoke, which the interface will call everytime an event get's raised.
Re: How do I use WithEvents?
Could I get you to post a sample project? Because I have no idea what you just said.
Re: How do I use WithEvents?
Quote:
Originally Posted by rm_03
get IConnectionPointContainer from the interface
How do you do this? Is it just a matter of casting the object?
1 Attachment(s)
Re: How do I use WithEvents?
Quote:
Originally Posted by penagate
How do you do this? Is it just a matter of casting the object?
Yes.
Like:
VB Code:
Set IConnectionPointContainer = object
Or:
VB Code:
IUnknown.QueryInterface IID_IConnectionPointContainer, IConnectionPointContainer
Quote:
Originally Posted by eyeRmonkey
Could I get you to post a sample project? Because I have no idea what you just said.
Sure :)
But it's rather complicated, I didn't want to use a type library.
Re: How do I use WithEvents?
What type library would make it easier?
Re: How do I use WithEvents?
One which defines the interfaces used in the example (IUnk/IDisp/ICPC/ICP).
I think it would shorten the code about 40-50%. I'm just too lazy :)