0
votes

My first post here.

I have c# .dll, to use with a scanner. I intend to use it with some legacy vb6 applications.

The .dll raises an event with the scanned code using RaiseArgs.

I am trying to write an .ocx library for use with VB6 apps.

To catch this event in an .ocx library I am trying to adapt this code:

Sub TestEvents()
    Dim Obj As New Class1
    ' Associate an event handler with an event.
    AddHandler Obj.Ev_Event, AddressOf EventHandler
    ' Call the method to raise the event.
    Obj.CauseSomeEvent()
    ' Stop handling events.
    RemoveHandler Obj.Ev_Event, AddressOf EventHandler
    ' This event will not be handled.
    Obj.CauseSomeEvent()
End Sub

Sub EventHandler()
    ' Handle the event.
    MsgBox("EventHandler caught event.")
End Sub

Public Class Class1
    ' Declare an event.
    Public Event Ev_Event()
    Sub CauseSomeEvent()
        ' Raise an event.
        RaiseEvent Ev_Event()
    End Sub
End Class

But I am getting Invalid use of AddressOf operator error when I call: AddHandler Obj.Ev_Event, AddressOf EventHandler

What could be probable cause of this error?

I suspect, that i am not going in the right direction with solving this task, so would it be a better way to approach this problem?

2
There is no AddHandler in VB6. Are you using VB.NET?GSerg
Hi! I am using vb6, and it seems I have missed, that I can use AddHandler only in .net... Would you point me in the direction how to solve my original problem: getting event into vb6?DontKnow
If you already have a COM-visible DLL with events that VB recognizes, declare an event handler like you normally would, in the form of a Private Sub ObjectName_EventName(). Otherwise see stackoverflow.com/q/39511528/11683.GSerg

2 Answers

3
votes

You have a mix of VB6 and VB.NET I think. In VB6, if you want to add an event handler for event on an object that you create yourself (as opposed to being added with the Forms designer) you would do it something like this:

Private WithEvents mObjectThatHasEvents As Class1

Private Sub StartListeningToEvents()

   If mObjectThatHasEvents Is Nothing Then
      Set mObjectThatHasEvents = New Class1
   End If

End Sub

Private Sub TriggerEvent()
   mObjectThatHasEvents.CauseSomeEvent
End Sub

'Assumes that Class1 has an event called "MyEvent".  
Private Sub mObjectThatHasEvents_MyEvent()
   MsgBox("EventHandler caught event.")
End Sub

Private Sub StopListeningToEvents()
   Set mObjectThatHasEvents = Nothing
End Sub

If you need to keep the event source object alive while not listening to its events, you will need a second reference to it via a variable that is not "WithEvents".

I should point out that the VB6 IDE will recognize the event source. In the code pane, use the dropdowns at the top to select the object and event. The IDE will stub them out for you automatically (just like buttons or anything else).

0
votes

tcarvin pretty much explained what you have to do but here's your code, adjusted:

Private WithEvents mObjWithEvents As Class1

Sub TestEvents()
    Dim Obj As New Class1

    Set mObjWithEvents = Obj

    ' Call the method to raise the event.
    Obj.CauseSomeEvent

    ' Stop handling events.
    Set mObjWithEvents = Nothing

    ' This event will not be handled.
    Obj.CauseSomeEvent

End Sub

Private Sub mObjWithEvents_EvEvent()
    ' Handle the event.
    MsgBox ("EventHandler caught event.")
End Sub

And your Class1:

Public Event EvEvent()

Public Sub CauseSomeEvent()
    ' Raise an event.
    RaiseEvent EvEvent
End Sub

Notice that Ev_Event was renamed to EvEvent.