在VBA中(对于MS Word)是否可以为自定义事件设置多个侦听器?我按照Custom Event not firing创建了一个自定义事件,但我真正想要的是拥有多个侦听器......
事件似乎合乎逻辑,事件只针对与之相关的实例触发,但我想在事件发生时触发几个地方的代码......是否可能?
Ref:
上方修改后的代码表格FactoryTest调用Factory。工厂举办活动。 FactoryTest监听事件并做出响应。
添加一个新的类模块并将其命名为Factory
var str = '1st qtr,2nd qtr,3rd qtr,4th qtr,';
var contains = str.indexOf('4th qtr') > -1;
alert('str contains 4th qtr? ' + contains);
添加另一个类模块,名称为FactoryTest
Public Event AfterInitialize()
Private Sub Class_Initialize()
End Sub
Public Sub test()
RaiseEvent AfterInitialize
End Sub
使用以下代码添加标准模块
Private WithEvents cFactory As Factory
Private Sub Class_Initialize()
Set cFactory = New Factory
cFactory.test
End Sub
Private Sub cFactory_AfterInitialize()
Debug.Print "after inialized..."
End Sub
答案 0 :(得分:1)
希望我能正确理解你的问题。如果您需要所有FactoryTests
响应单个事件,您可能必须将引用传递给一个公共Factory
到所有FactoryTests
个实例,而不是每个都创建一个新FactoryTest
个实例Factory
。 HTH
标准模块,这里创建了
FactoryTests
的常见实例,两个Factory
都响应Option Explicit Sub Main() Dim myFactory As Factory Set myFactory = New Factory Dim test1 As FactoryTest1 Dim test2 As FactoryTest2 Set test1 = New FactoryTest1 Set test2 = New FactoryTest2 Set test1.FactoryInstance = myFactory Set test2.FactoryInstance = myFactory myFactory.test End Sub
的同一事件
Factory
工厂测试类模块,创建更多的模块,每个模块都接收对一个公共
Option Explicit Private WithEvents cFactory As Factory Private Sub cFactory_AfterInitialize() Debug.Print "after inialized..." & VBA.TypeName(Me) End Sub Public Property Get FactoryInstance() As Factory Set FactoryInstance = cFactory End Property Public Property Set FactoryInstance(ByRef factoryObject As Factory) Set cFactory = factoryObject End Property
实例的引用
FactoryTest1
两个名为
的工厂测试类的输出FactoryTest2
和after inialized...FactoryTest1 after inialized...FactoryTest2
{{1}}