Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create custom events in .net

Tags:

.net

vb.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.

like image 642
Akhil K Nambiar Avatar asked Nov 21 '11 10:11

Akhil K Nambiar


3 Answers

This code will do the same thing as @user1057768's answer with a lot less fuss. Any exceptions thrown by an event handler will come back through the OnReceive method, so you can catch them there or elsewhere in your class.

Class MyClass
    Public Event Recieve As EventHandler 

    Protected Overridable Sub OnReceive(e As EventArgs)
        RaiseEvent Receive(Me, e)
    End Sub
End Class

You only need to use a Custom Event if you need special code to handle or raise the event.

like image 171
Hand-E-Food Avatar answered Sep 21 '22 15:09

Hand-E-Food


Public Custom Event recieve As EventHandler

    AddHandler(ByVal value As EventHandler)
        _handlers.Add(value)
    End AddHandler

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

    RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
        For Each handler As EventHandler In _handlers
            Try
                handler.Invoke(sender, e)
            Catch ex As Exception
                Debug.WriteLine("Exception while invoking event handler: " & ex.ToString())
            End Try
        Next
    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.

like image 25
Rajila Avatar answered Sep 20 '22 15:09

Rajila


In your control class you must declare a public event

Class MyControl

    Public Event MyReceivedEvent(ByVal AParameterIWantToSend As TheTypeOfThisParameter, ByVal AnotherParmeter As AnotherType, ...)

    ...

End Class

And when you use your control, you might hook the event in the xaml

< MyControl x:Name="MyControlName" MyReceivedEvent="AHandlerForThisEvent" ...  />

Or in the code :

AddHandler MyControlName.MyReceivedEvent, AddressOf AHandlerForThisEvent

Rq 1 : Your event might have no parameters. Rq2 : The handler can have the same parameters, or less if you don't care about some of them. It is a good habit to get them all. Rq3 : you might use Delegates to have a cleaner code, and warning/errors if you don't respect the Event's signature. like in

Public Delegate Sub MyReceivedHandler(ByVal NumberOfItemsReceived As Integer, ByVal QualityOfReception As String, ByRef ReceptionHandled As Boolean)

and then you hook your handler with

AddHandler MyControlName.MyReceivedEvent, New MyReveivedHandler(AddressOf AHandlerForThisEvent)

If you declared your event with this Delegate Type, the listeners will have to respect the EventHandler signature.

Public Event MyReceivedEvent As MyReceivedHandler

Rq4 : You might use ByRef parameters, just as in the latest example, so the parameter is sent to a listener, can be changed, and the next listener sees the updated value of this parameter.

Rq5 : you might use lambda as Event listener, in simple scenarios, like

AddHandler MyControlName.MyReceivedEvent, _
             New MyReceivedHandler(Sub(ByVal num As Integer , ByVal Q As String, ByRef RHandled As Boolean) _
                           MessageBox.Show(" Message receiveived  containing " & num & " items " & " of Quality : " & Q )
like image 45
GameAlchemist Avatar answered Sep 20 '22 15:09

GameAlchemist