System.ComponentModel.CancelEventArgs Class

Assembly: System.dll
Namespace: System.ComponentModel
Summary
Provides data for a cancelable event.
C# Syntax:
public class CancelEventArgs : EventArgs
Remarks
A cancelable event is raised by a component when it is about to perform an action that can be canceled, such as the Form.Closing event of a Form.

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

Example
The following example uses a CancelEventArgs and a CancelEventHandler to handle the Form.Closing event of a Form. This code assumes that you have created a Form with a class-level Boolean variable named myDataIsSaved .
// 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.");
       }
    }
 

    
See also:
System.ComponentModel Namespace | CancelEventHandler

System.ComponentModel.CancelEventArgs Member List:

Public Constructors
ctor #1 Overloaded:
.ctor()

Default constructor. This constructor is called by derived class constructors to initialize state in this type.
Initializes a new instance of the CancelEventArgs class with the CancelEventArgs.Cancel property set to false.
ctor #2 Overloaded:
.ctor(bool cancel)

Initializes a new instance of the CancelEventArgs class with the CancelEventArgs.Cancel property set to the given value.
Public Properties
Cancel Read-write

Gets or sets a value indicating whether the event should be canceled.
Public Methods
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.ComponentModel.CancelEventArgs Member Details

Overloaded ctor #1
Summary
Initializes a new instance of the CancelEventArgs class with the CancelEventArgs.Cancel property set to false.

Default constructor. This constructor is called by derived class constructors to initialize state in this type.
C# Syntax:
public CancelEventArgs();
See also:
CancelEventHandler

Return to top


Overloaded ctor #2
Summary
Initializes a new instance of the CancelEventArgs class with the CancelEventArgs.Cancel property set to the given value.
C# Syntax:
public CancelEventArgs(
   bool cancel
);
Parameters:

cancel

true to cancel the event; otherwise, false.

See also:
CancelEventHandler

Return to top


Property: Cancel (read-write)
Summary
Gets or sets a value indicating whether the event should be canceled.
C# Syntax:
public bool Cancel {get; set;}
Example
The following example uses a CancelEventArgs and a CancelEventHandler to handle the Form.Closing event of a Form. This code assumes that you have created a Form with a class-level Boolean variable named myDataIsSaved .
// 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.");
       }
    }
 

    

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~CancelEventArgs();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.