CancelEventArgs Class

Definición

Proporciona datos para un evento cancelable. Provides data for a cancelable event.

public class CancelEventArgs : EventArgs
Herencia
CancelEventArgs
Derivado

Ejemplos

En el ejemplo siguiente se usa CancelEventArgs y un CancelEventHandler para controlar la Closing eventos de un Form.The following example uses CancelEventArgs and a CancelEventHandler to handle the Closing event of a Form. Este código supone que ha creado un Form con un nivel de clase Boolean variable denominada isDataSaved.This code assumes that you have created a Form with a class-level Boolean variable named isDataSaved. También se supone que ha agregado una instrucción para invocar el OtherInitialize método desde el formulario Load método o el constructor (después de llamar a InitializeComponent).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).

private:
   // Call this method from the InitializeComponent() method of your form
   void OtherInitialize()
   {
      this->Closing += gcnew CancelEventHandler( this, &Form1::Form1_Cancel );
      this->myDataIsSaved = true;
   }

   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." );
      }
   }
// 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.");
       }
    }
 
' Call this method from the Load method of your form.
Private Sub OtherInitialize()
    ' Exchange commented line and note the difference.
    Me.isDataSaved = True
    'Me.isDataSaved = False
End Sub 'OtherInitialize

Private Sub Form1_Closing(sender As Object, e As _
   System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    If Not isDataSaved Then
        e.Cancel = True
        MessageBox.Show("You must save first.")
    Else
        e.Cancel = False
        MessageBox.Show("Goodbye.")
    End If
End Sub 'Form1_Closing

Comentarios

Componente provoca un evento cancelable cuando está a punto de realizar una acción que puede cancelarse, como el Closing eventos de un Form.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.

Nota

El Closing evento está en desuso y se ha reemplazado por FormClosing.The Closing event is deprecated and has been replaced by FormClosing. Se ofrece como un ejemplo aquí únicamente para ilustrar el uso de CancelEventArgs.It is offered as an example here only to illustrate the usage of CancelEventArgs.

CancelEventArgs proporciona el Cancel propiedad para indicar si se debe cancelar el evento.CancelEventArgs provides the Cancel property to indicate whether the event should be canceled.

Constructores

CancelEventArgs()

Inicializa una nueva instancia de la clase CancelEventArgs, con la propiedad Cancel establecida en false. Initializes a new instance of the CancelEventArgs class with the Cancel property set to false.

CancelEventArgs(Boolean)

Inicializa una nueva instancia de la clase CancelEventArgs, estableciendo la propiedad Cancel en el valor dado. Initializes a new instance of the CancelEventArgs class with the Cancel property set to the given value.

Propiedades

Cancel

Obtiene o establece un valor que indica si se debe cancelar el evento. Gets or sets a value indicating whether the event should be canceled.

Métodos

Equals(Object)

Determina si el objeto especificado es igual al objeto actual. Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Sirve como la función hash predeterminada. Serves as the default hash function.

(Inherited from Object)
GetType()

Obtiene el Type de la instancia actual. Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Crea una copia superficial del Object actual. Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Devuelve una cadena que representa el objeto actual. Returns a string that represents the current object.

(Inherited from Object)

Se aplica a

Consultar también