Akhil K Nambiar Akhil K Nambiar - 3 months ago 7x
Vb.net Question

create custom events in .net

I have created a control in vb.net. Now I want that control to send some message(fire an event called recieve) and the parent application that implements it will have to create an even listener called recieve and do actions accordingly.

Public Custom Event recieve As EventHandler

    AddHandler(ByVal value As EventHandler)
    End AddHandler

    RemoveHandler(ByVal value As EventHandler)
        If _handlers.Contains(value) Then
        End If
    End RemoveHandler

    RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
        For Each handler As EventHandler In _handlers
                handler.Invoke(sender, e)
            Catch ex As Exception
                Debug.WriteLine("Exception while invoking event handler: " & ex.ToString())
            End Try
    End RaiseEvent
End Event

Then you may raise the event by typing

Dim raise As New System.EventArgs
RaiseEvent recieve(sender, raise)

anywhere in your code.