Attaching an event handler multiple times
If you keep attaching event handlers then it will be raised once for each time you've attached the handler. This means:
- If you only need it to be raised once then assign one handler.
- If you attach the same handler 4 times then it will be called 4 times.
Looking at your code, instead of hooking into the CurrentCellActivated
event multiple times it would make more sense to subscribe to a general CellActivated
event once.
You can prevent it by unsubscribing before adding it,
Object.Event -= EventHandler(method);
Object.Event += EventHandler(method);
if can be done, prevent it by always subscribe events in one place say constructor
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