Hi all,
I have a library with some controls developed with VB.Net which I've
used for a while, now I've added an event to signal some data is changed,
but I can't find how to add event handler in the host application (VB6).
I can see regasm has automatically added a method add_<eventname> to my
control, and a type <eventname>eventhandler is now defined in my VBA
application, but I don't know the syntax to add the function handler...
Here is some code
********VB.net code
Public Interface iMyControl
Event DataChanged()
' some others properties and methods
End Interface
Class MyControl
' class initializzation omitted....
Implements iMyControl
Event DataChanged() Implements iMyControl.DataChanged
' others properties and methods implemented here
Public Event DataChanged
' code
' code
' code
Private Sub Edit()
' some code....
' if data is modified bDataChanged is set to true
If bDataChanged Then RaiseEvent DataChanged()
End Sub
End Class
End Namespace
********VBA
Public myCtrl As SomeApp.iMyControl
Sub Test()
Set myCtrl = New SomeApp.MyControl
myCtrl.add_DataChanged(myHandler) ' this is wrong I know but which is
the right way?
' some code use the control...
End Sub
Sub myHandler() as DataChanghedEventHandler
MsgBox "The data is changed !"
End Sub
Who can help?
Thanks!
Nicola
Christian Fröschlin - 07 Sep 2004 15:58 GMT
> Who can help?
The syntax in VB6 would be something like this:
Private WithEvents myCtrl As SomeApp.iMyControl
...
Set myCtrl = New SomeApp.MyControl
...
Private Sub myCtrl_DataChanged()
'Handler code
...
End Sub
However, I don't think that the event passes
from .NET to COM quite that easily. Try adding
some attributes to the declarations, e.g.
<InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)>
to iMyControl and
<ClassInterface(ClassInterfaceType.None), _
ComSourceInterfaces("SomeApp.iMyControl")> _
to MyControl