Question

I am little confused about setting events in .NET.

I have methods that is set to MouseDown as follows.

 if theTool = TMakerTool.tmtSelect then
 begin
   MouseDown += new System.Windows.Forms.MouseEventHandler(@SelectMouseDown);
   Cursor := Cursors.Arrow;
 end
 else
 begin
   MouseDown += new System.Windows.Forms.MouseEventHandler(@Maker_MouseDown);
   Cursor := Cursors.Cross;
 end;

Above code is called everytime a mouseup is fired. Because the code is just setting MouseDown event to a method, will it reset the old method that was already set or keep it and just another event method in its event list...

Was it helpful?

Solution

I could answer "it depends", however, if events are designed according to the .NET guidelines, adding an event the way you do, means adding it to a list of event handlers and not remove the old handlers. This is certainly true for all .NET's own events and should be true for just about any other well designed event.

If you want to remove an event from the list, use the -= operator.

If you define an event yourself, you can override this behavior by overriding the add-accessor or the remove-accessor. In such a case you are responsible for proper storing of the event handlers. However, this is hardly ever necessary to do by hand.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top