如何从 vb6 模块引发事件?

发布于 2024-11-09 09:57:03 字数 97 浏览 0 评论 0原文

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

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 to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羞稚 2024-11-16 09:57:03

引发事件:

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

(这是有道理的。)

是的,您的类上需要一个 Friend 方法,您可以调用该方法来从模块中引发事件:

Class:

Public Event Click()

Friend Sub OnClick()
  RaiseEvent Click
End Sub

Module:

someVar.OnClick

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
不忘初心 2024-11-16 09:57:03

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

首先定义一个回调接口:
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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文