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.
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.
Absolutely yes! You can fire a lightning event and that will be handled in each & every component which has a handler defined in it.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With