Raising Event with e.Cancel property

Can someone point me in the right direction on how to raise an event that passes an event arg that includes an e.Cancel property that I can then respond to to cancel what was going to happen?

Essentially I want to be able to add BeforeXXX events to my custom controls and cancel whatever it is if the user so choses.

Thanks!

[328 byte] By [codefund.com] at [2007-12-16]
# 1
Here is a routine for catching an application before it closes. You just need to handle the OnApplicationClosing event to cancel it out.

#region ApplicationClose Routine

private System.ComponentModel.CancelEventHandler onApplicationClosing;

public event System.ComponentModel.CancelEventHandler ApplicationClosing
{
add{onApplicationClosing += value;}
remove{onApplicationClosing -= value;}
}

protected virtual void OnApplicationClosing(System.ComponentModel.CancelEventArgs e)
{
if(onApplicationClosing != null)
{
onApplicationClosing(this, e);
}
}

private EventHandler onApplicationClosed;

public event EventHandler ApplicationClosed
{
add{onApplicationClosed += value;}
remove{onApplicationClosed -= value;}
}

protected virtual void OnApplicationClosed()
{
if(onApplicationClosed != null)
{
onApplicationClosed(this, EventArgs.Empty);
}
}

public void ApplicationClose()
{
System.ComponentModel.CancelEventArgs e = new System.ComponentModel.CancelEventArgs();

OnApplicationClosing(e);
if(!e.Cancel)
{
OnApplicationClosed();
}
}

#endregion

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 2
Ok, but doesn't the OnApplicationClosing go and raise Deligates that will return before they get values back?
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 3
Yes, in the excellent sample above, OnApplicationClosing will call any delegates hooked to the ApplicationClosing event. Should any of the delegates cancel the event, the ApplicationClosedEvent wouldn't be raised. Presumably any other work involved in closing the application would also not be performed.

- mike

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 4
Thanks for the compliment Mike :)
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 5
Great! Thanks guys, I just wanted to make sure that it wasn't going to do anything weird like roll through the code and keep going becasue it calls the delgate and then continues.
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Designer...