Events

Method 1

Custom events - raise event

In the class in which you want to raise the event, declare the event:
Public Event SetStatus(ByVal status As String)

Raise the event with the RaiseEvent function:
RaiseEvent SetStatus("Start Mailing")

Custom events - capture the event

While declaring the class, mention you want to capture it's events:
Private WithEvents mWorker As New Worker

Declare a sub that handles the event:
Private Sub WriteStatus(ByVal completedWork As Integer) Handles mWorker.SetStatus
The work is done in this sub.

Method 2

You can add and remove the handler:
AddHandler mWorker.WorkDone, AddressOf WriteStatus
RemoveHandler mWorker.WorkDone, AddressOf WriteStatus

Method 3

Using delegates:

Remarks

This is the way to give information from a dll to the main program.

In the dll:

Public Class StatusEventArgs
  Inherits EventArgs
  Private _type As Integer
  Private _description As String

  Public Property Type() As Integer
    Get
      Return _type
    End Get
    Set(ByVal value As Integer)
      _type = value
    End Set
  End Property

  Public Property Description() As String
    Get
      Return _description
    End Get
    Set(ByVal value As String)
      _description = value
    End Set
  End Property

  Public Sub New(ByVal type As Integer, ByVal description As String)
    Me._type = type
    Me._description = description
  End Sub
End Class

Public Shared Event SetStatus As EventHandler(Of StatusEventArgs)

RaiseEvent SetStatus(New Object, New StatusEventArgs(1, "Processing invoices "))

In the main program:

AddHandler InvoiceBL.SetStatus, AddressOf LogScreen

Private Sub LogScreen(ByVal sender As Object, ByVal e As StatusEventArgs)
...
End Sub

Creating custom events
Creating custom event class

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License