Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attaching an event handler multiple times

I am new to C#. I just wanted to know whether attaching event handler multiple times can cause unexpected result?

Actually in my application i am attaching an event handler to an event like

cr.ListControlPart.Grid.CurrentCellActivated += new EventHandler(Grid_CurrentCellActivated); 

and this line of code called multiple times in the code.

like image 919
BhushanK Avatar asked Oct 11 '13 09:10

BhushanK


People also ask

Can an event have multiple handlers?

You can assign as many handlers as you want to an event using addEventListener(). addEventListener() works in any web browser that supports DOM Level 2.

Can we use same event handlers on multiple components?

Absolutely yes! You can fire a lightning event and that will be handled in each & every component which has a handler defined in it.

Can you have multiple event listeners for the same event?

The addEventListener() methodYou can add many event handlers to one element. You can add many event handlers of the same type to one element, i.e two "click" events. You can add event listeners to any DOM object not only HTML elements.

How many ways can you attach an event handler to a DOM element?

There are two ways to add an event handler to any element, the first is using the addEventListener method and another is using the event attributes.


1 Answers

Try it yourself:

static class Program
{
    static event EventHandler MyEvent;

    static void Main()
    {
        // registering event
        MyEvent += Program_MyEvent;
        MyEvent += Program_MyEvent;
        MyEvent += Program_MyEvent;
        MyEvent += Program_MyEvent;
        MyEvent += Program_MyEvent;

        // invoke event
        MyEvent(null, EventArgs.Empty);
        Console.ReadKey();
    }

    static void Program_MyEvent(object sender, EventArgs e)
    {
        Console.WriteLine("MyEvent fired");
    }
}

Output:

MyEvent fired
MyEvent fired
MyEvent fired
MyEvent fired
MyEvent fired
like image 123
Alessandro D'Andria Avatar answered Oct 04 '22 16:10

Alessandro D'Andria