0 out of 7 rated this helpful - Rate this topic

CancelEventArgs Class

Provides data for a cancelable event.

System.Object
  System.EventArgs
    System.ComponentModel.CancelEventArgs
      More...

Namespace:  System.ComponentModel
Assembly:  System (in System.dll)
[HostProtectionAttribute(SecurityAction.LinkDemand, SharedState = true)]
public class CancelEventArgs : EventArgs

The CancelEventArgs type exposes the following members.

  NameDescription
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryCancelEventArgs()Initializes a new instance of the CancelEventArgs class with the Cancel property set to false.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryCancelEventArgs(Boolean)Initializes a new instance of the CancelEventArgs class with the Cancel property set to the given value.
Top
  NameDescription
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryCancelGets or sets a value indicating whether the event should be canceled.
Top
  NameDescription
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibraryFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibraryMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryToStringReturns a string that represents the current object. (Inherited from Object.)
Top

A cancelable event is raised by a component when it is about to perform an action that can be canceled, such as the Closing event of a Form.

NoteNote

The Closing event is deprecated and has been replaced by FormClosing. It is offered as an example here only to illustrate the usage of CancelEventArgs.

CancelEventArgs provides the Cancel property to indicate whether the event should be canceled.

NoteNote

The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: SharedState. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

The following example uses CancelEventArgs and a CancelEventHandler to handle the Closing event of a Form. This code assumes that you have created a Form with a class-level Boolean variable named isDataSaved. It also assumes that you have added a statement to invoke the OtherInitialize method from the form's Load method or the constructor (after the call to InitializeComponent).


// Call this method from the constructor of your form
    private void OtherInitialize() {
       this.Closing += new CancelEventHandler(this.Form1_Closing);
       // Exchange commented line and note the difference.
       this.isDataSaved = true;
       //this.isDataSaved = false;
    }

    private void Form1_Closing(Object sender, CancelEventArgs e) {
       if (!isDataSaved) {
          e.Cancel = true;
          MessageBox.Show("You must save first.");
       }
       else {
          e.Cancel = false;
          MessageBox.Show("Goodbye.");
       }
    }
 


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
System.Object
  System.EventArgs
    System.ComponentModel.CancelEventArgs
      Microsoft.VisualBasic.ApplicationServices.StartupEventArgs
      System.ComponentModel.DoWorkEventArgs
      System.Configuration.SettingChangingEventArgs
      System.Drawing.Printing.PrintEventArgs
      System.Web.UI.WebControls.DetailsViewDeleteEventArgs
      System.Web.UI.WebControls.DetailsViewInsertEventArgs
      System.Web.UI.WebControls.DetailsViewModeEventArgs
      System.Web.UI.WebControls.DetailsViewPageEventArgs
      System.Web.UI.WebControls.DetailsViewUpdateEventArgs
      System.Web.UI.WebControls.EntityDataSourceChangingEventArgs
      System.Web.UI.WebControls.EntityDataSourceContextDisposingEventArgs
      System.Web.UI.WebControls.EntityDataSourceSelectingEventArgs
      System.Web.UI.WebControls.FormViewDeleteEventArgs
      System.Web.UI.WebControls.FormViewInsertEventArgs
      System.Web.UI.WebControls.FormViewModeEventArgs
      System.Web.UI.WebControls.FormViewPageEventArgs
      System.Web.UI.WebControls.FormViewUpdateEventArgs
      System.Web.UI.WebControls.GridViewCancelEditEventArgs
      System.Web.UI.WebControls.GridViewDeleteEventArgs
      System.Web.UI.WebControls.GridViewEditEventArgs
      System.Web.UI.WebControls.GridViewPageEventArgs
      System.Web.UI.WebControls.GridViewSelectEventArgs
      System.Web.UI.WebControls.GridViewSortEventArgs
      System.Web.UI.WebControls.GridViewUpdateEventArgs
      System.Web.UI.WebControls.LinqDataSourceDeleteEventArgs
      System.Web.UI.WebControls.LinqDataSourceDisposeEventArgs
      System.Web.UI.WebControls.LinqDataSourceInsertEventArgs
      System.Web.UI.WebControls.LinqDataSourceSelectEventArgs
      System.Web.UI.WebControls.LinqDataSourceUpdateEventArgs
      System.Web.UI.WebControls.ListViewCancelEventArgs
      System.Web.UI.WebControls.ListViewDeleteEventArgs
      System.Web.UI.WebControls.ListViewEditEventArgs
      System.Web.UI.WebControls.ListViewInsertEventArgs
      System.Web.UI.WebControls.ListViewSelectEventArgs
      System.Web.UI.WebControls.ListViewSortEventArgs
      System.Web.UI.WebControls.ListViewUpdateEventArgs
      System.Web.UI.WebControls.ObjectDataSourceDisposingEventArgs
      System.Web.UI.WebControls.ObjectDataSourceFilteringEventArgs
      System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs
      System.Web.UI.WebControls.SqlDataSourceCommandEventArgs
      System.Web.UI.WebControls.SqlDataSourceFilteringEventArgs
      System.Web.UI.WebControls.WebParts.WebPartCancelEventArgs
      System.Web.UI.WebControls.WebParts.WebPartConnectionsCancelEventArgs
      System.Web.UI.WebControls.WebParts.WebPartDisplayModeCancelEventArgs
      System.Windows.Controls.InkCanvasSelectionChangingEventArgs
      System.Windows.Controls.InkCanvasSelectionEditingEventArgs
      System.Windows.Controls.InkCanvasStrokeErasingEventArgs
      System.Windows.Forms.BindingCompleteEventArgs
      System.Windows.Forms.ColumnReorderedEventArgs
      System.Windows.Forms.ColumnWidthChangingEventArgs
      System.Windows.Forms.DataGridViewCellCancelEventArgs
      System.Windows.Forms.DataGridViewCellValidatingEventArgs
      System.Windows.Forms.DataGridViewRowCancelEventArgs
      System.Windows.Forms.FormClosingEventArgs
      System.Windows.Forms.InputLanguageChangingEventArgs
      System.Windows.Forms.PopupEventArgs
      System.Windows.Forms.SplitterCancelEventArgs
      System.Windows.Forms.TabControlCancelEventArgs
      System.Windows.Forms.ToolStripDropDownClosingEventArgs
      System.Windows.Forms.TreeViewCancelEventArgs
      System.Windows.Forms.WebBrowserNavigatingEventArgs
      System.Windows.Navigation.NavigatingCancelEventArgs
      System.Windows.SessionEndingCancelEventArgs
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.