I have a WinForms form that won’t close. In OnFormClosing, e.Cancel is set to true. I am guessing that some object in my application has bound to the Closing or FormClosing event, and is blocking the close. To find out, I’d like to determine what delegates are bound to one of these events.
Is there a way to determine the list of handlers bound to an event? Ideally I would do this via the Visual Studio debugger, but can write code in the application to find the handlers if necessary. Understanding that an event is like a hidden private field, I’ve navigated through the Debugger to the ‘Non-Public Fields’ for the ‘Windows.Forms.Form’ ancestor of my form, but to no avail.
In short, you’re not meant to do this – but for debugging purposes…
An event is often backed by a private field – but not with controls; they use the
EventHandlerListapproach. You would have to access the form’s protectedEventsmember, looking for the object mapped to the (private) EVENT_FORMCLOSING object.Once you have the
FormClosingEventHandler,GetInvocationListshould do the job.