Freigeben über


CancelEventArgs.Cancel-Eigenschaft

Ruft einen Wert ab, der angibt, ob das Ereignis abgebrochen werden soll, oder legt diesen fest.

Namespace: System.ComponentModel
Assembly: System (in system.dll)

Syntax

'Declaration
Public Property Cancel As Boolean
'Usage
Dim instance As CancelEventArgs
Dim value As Boolean

value = instance.Cancel

instance.Cancel = value
public bool Cancel { get; set; }
public:
property bool Cancel {
    bool get ();
    void set (bool value);
}
/** @property */
public boolean get_Cancel ()

/** @property */
public void set_Cancel (boolean value)
public function get Cancel () : boolean

public function set Cancel (value : boolean)

Eigenschaftenwert

true, wenn das Ereignis abgebrochen werden soll, andernfalls false.

Beispiel

Im folgenden Beispiel werden CancelEventArgs und CancelEventHandler verwendet, um das Closing-Ereignis eines Form zu behandeln. In diesem Codebeispiel wird davon ausgegangen, dass ein Form mit einer Boolean-Variablen auf Klassenebene und dem Namen isDataSaved erstellt wurde. Außerdem wird davon ausgegangen, dass Sie eine Anweisung hinzugefügt haben, die die OtherInitialize-Methode aus der Load-Methode oder aus dem Konstruktor des Formulars (nach dem Aufruf von InitializeComponent) aufruft.

' 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
// 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.");
       }
    }
 
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." );
      }
   }
// Calls this method from the InitializeComponent() method of your form
private void OtherInitialize()
{
    this.add_Closing(new CancelEventHandler(this.Form1_Cancel));
    this.myDataIsSaved = (boolean)new System.Boolean();
    this.myDataIsSaved = true;
} //OtherInitialize

protected void Form1_Cancel(Object sender, CancelEventArgs e)
{
    if (!(myDataIsSaved)) {
        e.set_Cancel(true);
        MessageBox.Show("You must save first.");
    }
    else {
        e.set_Cancel(false);
        MessageBox.Show("Goodbye.");
    }
} //Form1_Cancel

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

CancelEventArgs-Klasse
CancelEventArgs-Member
System.ComponentModel-Namespace