从模块捕获事件

时间:2013-09-02 22:49:50

标签: vb.net

我在一个模块中发送电子邮件的功能,我已经声明了我的自定义事件:

Public Module myModule
    Public Event emailCompleted()

在该模块中,我有完成发送电子邮件时触发的功能:

Private Sub SendCompletedCallback(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
    If e.Error Is Nothing Then
        RaiseEvent emailCompleted()
    End If
End Sub

正如所料,所有这一切都运作良好。

问题是我不知道如何在我的主程序中捕获该事件,该程序调用sendemail函数 怎么做?

1 个答案:

答案 0 :(得分:1)

您只能从类中引发事件并在该类之外处理它们:

Public Class myModule
    Public Event emailCompleted()
    Private Sub SendCompletedCallback(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
        If e.Error Is Nothing Then
            RaiseEvent emailCompleted()
        End If
    End Sub

并在您的调用代码模块中:

dim WithEvents myModuleObject as MyModule

Sub HandleEvent() Handles myModuleObject.emailCompleted
...