unable to receive .NET event in VB6

G

Guest

Hi all,

I have created vb.net user control that has to be used by vb6 form.
Everything goes well with putting the vb.net user control on the VB6 form
until I want to receive any event from my control. The event handler is
displayed on VB6 IDE combo and you can create a sub for it as usual, but when
I run the vb6 form to test it, it won't work.

I have spent too much time til now with that without finding solution.
Can anybody help me ?

Thanks a lot,

Marek
(e-mail address removed)

The codes are as follows:

-----------------------------------------------------------
VB.NET user control
-----------------------------------------------------------

Imports System.Runtime.InteropServices
Imports System.Text
Imports Microsoft.Win32
Imports System.Reflection

Public Delegate Sub ButtonPressedDelegate()

' Step 1: Defines an event sink interface (ButtonEvents) to be
' implemented by the COM sink.
<GuidAttribute("1c646f53-ce4e-4bb6-8a67-f94a13172431"), _
InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)> _
Public Interface IButtonEvents
Sub ButtonPressed()
End Interface

<Guid("f1bc2b4d-9844-469d-849c-cfe7f456b989"), _
ComSourceInterfaces("vbNetClarifyTest.IButtonEvents, vbNetClarifyTest")> _
Public Class TestButton

'#Region "COM GUIDs"
' ' These GUIDs provide the COM identity for this class
' ' and its COM interfaces. If you change them, existing
' ' clients will no longer be able to access the class.
' Public Const InterfaceId As String =
"e32ccb34-07c6-4db3-aa0f-1e5b364d513c"
' Public Const EventsId As String = ""
'#End Region

Public Event ButtonPressed As ButtonPressedDelegate

' A creatable COM class must have a Public Sub New()
' with no parameters, otherwise, the class will not be
' registered in the COM registry and cannot be created
' via CreateObject.
Public Sub New()
MyBase.New()
Call InitializeComponent()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
RaiseEvent ButtonPressed()
End Sub

<ComRegisterFunction()> _
Private Shared Sub ComRegister(ByVal t As Type)

Dim keyName As String = "CLSID\" & t.GUID.ToString("B")
Dim key As RegistryKey = Registry.ClassesRoot.OpenSubKey(keyName,
True)
key.CreateSubKey("Control").Close()
Dim subkey As RegistryKey = key.CreateSubKey("MiscStatus")
subkey.SetValue("", "131457")
subkey = key.CreateSubKey("TypeLib")
Dim libid As Guid = Marshal.GetTypeLibGuidForAssembly(t.Assembly)
subkey.SetValue("", libid.ToString("B"))
subkey = key.CreateSubKey("Version")
Dim ver As Version = t.Assembly.GetName().Version
Dim version As String = String.Format("{0}.{1}", ver.Major, ver.Minor)
If version = "0.0" Then version = "1.0"
subkey.SetValue("", version)

End Sub

' This is called when unregistering
<ComUnregisterFunction()> _
Private Shared Sub ComUnregister(ByVal t As Type)
' Delete entire CLSID\{clsid} subtree
Dim keyName As String = "CLSID\" + t.GUID.ToString("B")
Registry.ClassesRoot.DeleteSubKeyTree(keyName)
End Sub

End Class
 
G

Guest

Hi all,

as I can see no reply to my question after several day.
Beside waiting for any reply I have searched for solution.
And I found one. It is a dirty solution but works,and actualy it is a Java
style solution.

Solution is as follows:

Create one interface with method you vant to use as event for exmple like
this:

Imports System.Runtime.InteropServices
Imports EventInfoSupport

<ComVisible(True)> _
Public Interface ICICCoreEventsListener
<ComVisible(True)> _
Sub ExtendedObjectEvent(ByVal info As EventInfoObject)
End Interface

Then in your user control that will fire an event add two methods and
declare one variable as ArrayList. Variable will hold an listeners that are
listening for method call and metioned methods are used for adding and
removing listeners from the list of listeners.

Private mlisteners As New ArrayList

<ComVisible(True)> _
Public ReadOnly Property Listeners() As ArrayList
Get
Return mlisteners
End Get
End Property

<ComVisible(True)> _
Public Sub AddEventListener(ByVal listen As ICICCoreEventsListener)
If listen Is Nothing Then
Return
End If
If (Not Listeners.Contains(listen)) Then
Call Listeners.Add(listen)
End If
End Sub

<ComVisible(True)> _
Public Sub RemoveEventListener(ByVal listen As ICICCoreEventsListener)
If listen Is Nothing Then Return
If (Listeners.Contains(listen)) Then
Call Listeners.Remove(listen)
End If
End Sub

After that on place when you want to fire an event add following:

For Each l As ICICCoreEventsListener In Listeners
If Not l Is Nothing Then
l.ExtendedObjectEvent(info)
End If
Next

So this is an VB.NET 2003/5 side. Now here is the VB6 side:

Implements ICICCoreEventsListener

Private Sub ICICCoreEventsListener_ExtendedObjectEvent(ByVal info As
EventInfoSupport.EventInfoObject)
' do something here after catching of our pseudo event
End Sub

So this is my solution that worked fine also with combination of ClarifyCRM
screen - VB6 control - VB.NET control. There are limitations on CrarifyCRM
side of version 10, that forced me to use VB6 control between Clarify screen
and VB.NET user control.

The class EventInfoSupport.EventInfoObject is own implementation of
EventInfo class because the VBExtender EventInfo object is not more supported
in .NET and is is not the same as System.Refrection.EventInfo object. So I
had to make such own implementation to be compatible with VB6 and VB.NET.

I am not so much satisfied with this forum because of answering speed and MS
itself because of backward compatibility support for events. There is problem
with events also on C# side as well.

Hope it will helps to others too. In case of additional question write me an
email to (e-mail address removed).

Bye,

Marek
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top