Replicate CancelEventArgs behaviour?
-
How can one replicate the Close() event with its corresponding CancelEventArgs? I.e. how to find out from the code firing the event what the receiver set this attribute to? Does anyone know and can help clarify this procedure to me...
-
How can one replicate the Close() event with its corresponding CancelEventArgs? I.e. how to find out from the code firing the event what the receiver set this attribute to? Does anyone know and can help clarify this procedure to me...
If there's just a single event handler attached, then it's fairly easy: In your class you need:
public delegate MyCancelHandler(bool cancel); public event MyCancelHandler AnEventThatCanBeCancelled; // Later on when you want the event to fire if (AnEventThatCanBeCancelled != null) { bool cancel = false; AnEventThatCanBeCancelled(cancel); if (cancel) { MessageBox.Show("Event has been cancelled by a listener"); } else { MessageBox.Show("Event has NOT been cancelled, go on with whatever you want"); } }
If there's more than one listener attached, then you'll only get the cancel info of the last listener notified. In this case you'll have to go through all registered delegates (usingMulticastDelegate.GetInvocationList()
) and check all of them if anyone wants to cancel your event. Regards, mav -
If there's just a single event handler attached, then it's fairly easy: In your class you need:
public delegate MyCancelHandler(bool cancel); public event MyCancelHandler AnEventThatCanBeCancelled; // Later on when you want the event to fire if (AnEventThatCanBeCancelled != null) { bool cancel = false; AnEventThatCanBeCancelled(cancel); if (cancel) { MessageBox.Show("Event has been cancelled by a listener"); } else { MessageBox.Show("Event has NOT been cancelled, go on with whatever you want"); } }
If there's more than one listener attached, then you'll only get the cancel info of the last listener notified. In this case you'll have to go through all registered delegates (usingMulticastDelegate.GetInvocationList()
) and check all of them if anyone wants to cancel your event. Regards, mavThat won't work.
bool
(System.Boolean
) is a value type, and you'd need to declare the delegate parameter asout
orref
. Besides, it's far easier to just reuseCancelEventHandler
. Since it uses theCancelEventArgs
which has aCancel
property (that will keep the value), you don't need to declare your own event. Also, while it's not required, events should typically follow theEventHandler
signature and the second parameter should (agian, not required to) derive fromEventArgs
.CancelEventArgs
does, of course, extend this class. If you want to know what a specific callee of the callback set theCancelEventArgs.Cancel
property to, your past paragraph is correct.Microsoft MVP, Visual C# My Articles
-
That won't work.
bool
(System.Boolean
) is a value type, and you'd need to declare the delegate parameter asout
orref
. Besides, it's far easier to just reuseCancelEventHandler
. Since it uses theCancelEventArgs
which has aCancel
property (that will keep the value), you don't need to declare your own event. Also, while it's not required, events should typically follow theEventHandler
signature and the second parameter should (agian, not required to) derive fromEventArgs
.CancelEventArgs
does, of course, extend this class. If you want to know what a specific callee of the callback set theCancelEventArgs.Cancel
property to, your past paragraph is correct.Microsoft MVP, Visual C# My Articles
Of course you're right, Heath.:doh: This is what you get when you don't actually try out your own suggestions...