CancelEventArgs.Cancel Propiedad

Definición

Obtiene o establece un valor que indica si se debe cancelar el evento.

C#
public bool Cancel { get; set; }

Valor de propiedad

truesi se debe cancelar el evento; en caso contrario, false.

Ejemplos

En el ejemplo siguiente se usa CancelEventArgs y un CancelEventHandler para controlar el FormClosing evento de .Form En este código se supone que ha creado un Form con una variable de nivel Boolean de clase denominada isDataSaved. También supone que ha agregado una instrucción para invocar el método desde el OtherInitialize método del Load formulario o el constructor (después de la llamada a InitializeComponent).

C#
// 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.");
       }
    }

Se aplica a

Producto Versiones
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0