开发者

How to raise an event from vb6 module?

I have developed a custom visual basic 6 control and declared a few custom events. Is it possible in vb6 to raise these events from a module or I need to implement a special "proxy" methods in my control 开发者_C百科to do this?


RaiseEvent:

Compile error:
Only valid in object module.

(Which makes sense.)

Yes, you need a Friend method on your class that you would call to raise events from your module:

Class:

Public Event Click()

Friend Sub OnClick()
  RaiseEvent Click
End Sub

Module:

someVar.OnClick


Perhaps not entirely the answer you are looking for, but it is possible to use Event-like procedures from plain Modules:

First define a Callback Interface: IEventsClient (Class Module):

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

The main Form:

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜