How to use WithEvents keyword with global variable?

后端 未结 4 541
无人及你
无人及你 2021-01-05 13:24

I am trying to declare a variable in a VB6 module as follows:

Public WithEvents MyObject As MyClass

The help files say that WithEvent

4条回答
  •  滥情空心
    2021-01-05 14:12

    Write a class that accepts your global object as a parameter and sinks its events.

    ' Class MySink
    Private WithEvents m_oSink As MyClass
    
    Friend Sub frInit(oSink As MyClass)
        Set m_oSink = oSink
    End Sub
    
    Private Sub m_oSink_MyEvent()
        '--- implement event
    End Sub
    

    Create an instance of this class in your .bas module.

    Public g_oMyObject AS MyClass
    Private m_oMySink As MySink
    
    Sub Main()
        Set g_oMyObject = New MyClass
        Set m_oMySink = New MySink
        m_oMySink.frInit g_oMyObject
    End Sub
    

提交回复
热议问题