Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to raise event using addHandler

I am comfortable with Vb.Net events and handlers. Can anybody will help me with how to create event handlers in c#, and raise events.

like image 702
Mitesh Avatar asked Sep 28 '09 18:09

Mitesh


People also ask

How do you raise an event?

Typically, to raise an event, you add a method that is marked as protected and virtual (in C#) or Protected and Overridable (in Visual Basic). Name this method On EventName; for example, OnDataReceived .

How do you raise an event in Visual Basic?

To raise a custom event, you must declare it in your control and call the RaiseEvent method. Note that the same event may be raised from many different places in the control's code. Raising a custom event from within a control is as simple as raising an event from within a class.

How do you call an event in Visual Basic?

Call an event handler using AddHandlerExecute an AddHandler statement to dynamically connect the event-handling Sub procedure with the event. When the event occurs, Visual Basic automatically calls the Sub procedure. Your code can use a RaiseEvent statement to make the event occur.


3 Answers

Developers who know only C#, or only VB.Net, may not know that this is one of the larger differences between VB.NET and C#.

I will shamelesssly copy this nice explanation of VB events: VB uses a declarative syntax for attaching events. The Handles clause appears on the code that will handle the event. When appropriate, multiple methods can handle the same event, and multiple events can be handled by the same method. Use of the Handles clause relies on the WithEvents modifier appearing on the declaration of the underlying variable such as a button. You can also attach property handlers using the AddHandler keyword, and remove them with RemoveHandler. For example

Friend WithEvents TextBox1 As System.Windows.Forms.TextBox   

Private Sub TextBox1_Leave(ByVal sender As System.Object, _
  ByVal e As System.EventArgs) Handles TextBox1.Leave
  'Do Stuff '
End Sub

In C# you can't use the declarative syntax. You use += which is overloaded to act like the VB.Net AddHandler. Here's an example shamelessly stolen from tster's answer:

public MyClass()
{
    InitializeComponent();
    textBox1.Leave += new EventHandler(testBox1_Leave);
}

void testBox1_Leave(object sender, EventArgs e)
{
  //Do Stuff
}
like image 167
MarkJ Avatar answered Nov 15 '22 20:11

MarkJ


In C# 2 and up you add event handlers like this:

yourObject.Event += someMethodGroup;

Where the signature of someMethodGroup matches the delegate signature of yourObject.Event.

In C# 1 you need to explicitly create an event handler like this:

yourObject.Event += new EventHandler(someMethodGroup);

and now the signatures of the method group, event, and EventHandler must match.

like image 35
Andrew Hare Avatar answered Nov 15 '22 19:11

Andrew Hare


    public MyClass()
    {
        InitializeComponent();
        textBox1.LostFocus += new EventHandler(testBox1_LostFocus);
    }

    void testBox1_LostFocus(object sender, EventArgs e)
    {
        // do stuff
    }
like image 45
tster Avatar answered Nov 15 '22 19:11

tster