Cancel Events
-
I've been working with my own custom events for controls and thought I had it figured out until I wanted to implement "Before" events(you know like BeforeExpand in TreeView). This kind of event will have a "Cancel" or "Handled" flag to indicate to the sender that the event was canceled (or handled) by the subscriber to the event. My question is, as the sender of the event, how do I know about the Canceling or handling? I understand that the eventargs object will have a boolean value to indicate this but how does that information get back to the sender? Thanks in advance, Eric
-
I've been working with my own custom events for controls and thought I had it figured out until I wanted to implement "Before" events(you know like BeforeExpand in TreeView). This kind of event will have a "Cancel" or "Handled" flag to indicate to the sender that the event was canceled (or handled) by the subscriber to the event. My question is, as the sender of the event, how do I know about the Canceling or handling? I understand that the eventargs object will have a boolean value to indicate this but how does that information get back to the sender? Thanks in advance, Eric
You could just use
CancelEventArgs
. ;P xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots -
I've been working with my own custom events for controls and thought I had it figured out until I wanted to implement "Before" events(you know like BeforeExpand in TreeView). This kind of event will have a "Cancel" or "Handled" flag to indicate to the sender that the event was canceled (or handled) by the subscriber to the event. My question is, as the sender of the event, how do I know about the Canceling or handling? I understand that the eventargs object will have a boolean value to indicate this but how does that information get back to the sender? Thanks in advance, Eric
Beringer wrote: but how does that information get back to the sender Because the event args are sent as a reference, so when control returns to your event handler the boolean value will be retained. assuming the following FooEventArgs have a "CancelFoo" boolean property
private BeforeFooDelegate beforeFoo private void onBeforeFoo(FooEventArgs e) { if(beforeFoo != null) beforeFoo(this,e); // listeners can set e.CancelFoo = true to cancel the execution of Foo if(!e.CancelFoo) // if it is not cancelled - do Foo this.Foo(); }
-
I've been working with my own custom events for controls and thought I had it figured out until I wanted to implement "Before" events(you know like BeforeExpand in TreeView). This kind of event will have a "Cancel" or "Handled" flag to indicate to the sender that the event was canceled (or handled) by the subscriber to the event. My question is, as the sender of the event, how do I know about the Canceling or handling? I understand that the eventargs object will have a boolean value to indicate this but how does that information get back to the sender? Thanks in advance, Eric
Cancel Supported by the .NET Compact Framework. Gets or sets a value indicating whether the event should be canceled. // Calls this method from the InitializeComponent() method of your form private void OtherInitialize() { this.Closing += new CancelEventHandler(this.Form1_Cancel); this.myDataIsSaved = new Boolean(); this.myDataIsSaved = true; } protected void Form1_Cancel (Object sender, CancelEventArgs e) { if (!myDataIsSaved) { e.Cancel = true; MessageBox.Show("You must save first."); } else { e.Cancel = false; MessageBox.Show("Goodbye."); } } mahes
-
Cancel Supported by the .NET Compact Framework. Gets or sets a value indicating whether the event should be canceled. // Calls this method from the InitializeComponent() method of your form private void OtherInitialize() { this.Closing += new CancelEventHandler(this.Form1_Cancel); this.myDataIsSaved = new Boolean(); this.myDataIsSaved = true; } protected void Form1_Cancel (Object sender, CancelEventArgs e) { if (!myDataIsSaved) { e.Cancel = true; MessageBox.Show("You must save first."); } else { e.Cancel = false; MessageBox.Show("Goodbye."); } } mahes
-
Beringer wrote: but how does that information get back to the sender Because the event args are sent as a reference, so when control returns to your event handler the boolean value will be retained. assuming the following FooEventArgs have a "CancelFoo" boolean property
private BeforeFooDelegate beforeFoo private void onBeforeFoo(FooEventArgs e) { if(beforeFoo != null) beforeFoo(this,e); // listeners can set e.CancelFoo = true to cancel the execution of Foo if(!e.CancelFoo) // if it is not cancelled - do Foo this.Foo(); }