views:

116

answers:

2

I'm having a strange bug where an undesired event is firing. I can trace into the code and see that the "onRowLeave" event handler is being removed using the -= syntax, then the next line a DataSource is changed which causes the "OnRowLeave" handler to fire!

So it seems the removal is not working.

I can tell it's not being added in again somehow because I have a breakpoint in all the places where it's added with += and it's not hitting those.

I'm thinking that maybe it's being added twice and only removed once... would that cause it? Is there any way I can see the handlers in the debugger?

+1  A: 

->> I'm thinking that maybe it's being added twice and only removed once... would that cause it?

That is correct.

->> Is there any way I can see the handlers in the debugger?

I do not think that is possible. Also, if you do not know the number of times the event handler was added, taking them off is not a straight forward task. I presume you can use reflection to achieve this.

I would instead recommend you study the code and understand where and why you have multiple event registrations, so that you can manually remove them if need be.

(You might want to note (as suggested by nobugz), that OnRowLeave is a method of the DataGridView and is not an event. This method is not exposed to the user but instead is responsible for raising the RowLeave event which the user can trap. I am not quite sure why you use the term OnRowLeave)

Preets
A: 

When I have had lingering event handler problems like this, I usually provide my own event add / remove methods and track the event handlers in my own container along with other debug data if it seems useful. This allows inspection of the collection in the debugger, and the additional data can be pretty useful if it associates a System.Diagnostics.StackTrace object for each event that is added in this manner.

Eric Cosky