如何从vb6模块中引发事件?

时间:2011-05-23 18:14:08

标签: vb6

我开发了一个自定义的visual basic 6控件并声明了一些自定义事件。是否可以在vb6中从模块中引发这些事件,或者我需要在我的控件中实现一个特殊的“代理”方法来执行此操作?

2 个答案:

答案 0 :(得分:7)

<强>的RaiseEvent:

  

编译错误:
  仅在对象模块中有效。

(这是有道理的。)

是的,您需要在课程中使用Friend方法来调用模块中的事件:

类:

Public Event Click()

Friend Sub OnClick()
  RaiseEvent Click
End Sub

模块:

someVar.OnClick

答案 1 :(得分:3)

或许不完全是您正在寻找的答案,但可以使用普通模块中的类似事件的程序:

首先定义一个回调接口: IEventsClient(类模块):

Option Explicit

Public Sub PropertyChanged(sender As Object, property As String)
End Sub

MyModule的:

Option Explicit

Public EventClients As Collection

Public Sub OnPropertyChanged(property As String)
    Dim eventsClient As IEventsClient
    Dim element As Variant

    For Each element In EventClients
        Set eventsClient = element
        eventsClient.PropertyChanged MyControl, property
    Next

End Sub

Public Sub RaiseSomePropertyChanged()
    OnPropertyChanged "SomeProperty"
End Sub

主要表格:

Option Explicit
Implements IEventsClient

Private Sub Form_Load()
    'Entry point of the application'
    Set MyModule.EventClients = New Collection
    MyModule.EventClients.Add Me
End Sub

Private Sub IEventsClient_PropertyChanged(sender As Object, property As String)
    If TypeOf sender Is MyControl Then
        Select Case property
            Case "SomeProperty"
            '   DoSomething'
        End Select
    End If
End Sub