CancelEventArgs.Cancel CancelEventArgs.Cancel CancelEventArgs.Cancel CancelEventArgs.Cancel Property

정의

이벤트를 취소해야 할지 여부를 나타내는 값을 가져오거나 설정합니다.Gets or sets a value indicating whether the event should be canceled.

public:
 property bool Cancel { bool get(); void set(bool value); };
public bool Cancel { get; set; }
member this.Cancel : bool with get, set
Public Property Cancel As Boolean

속성 값

이벤트를 취소해야 하면 true이고, 그렇지 않으면 false입니다.true if the event should be canceled; otherwise, false.

예제

다음 예제에서는 CancelEventArgsCancelEventHandler 처리 하는 FormClosing 이벤트는 Form합니다.The following example uses CancelEventArgs and a CancelEventHandler to handle the FormClosing event of a Form. 이 코드를 만들었다고 가정 합니다는 Form 클래스 수준 Boolean 라는 변수에 isDataSaved합니다.This code assumes that you have created a Form with a class-level Boolean variable named isDataSaved. 또한 호출 하는 문을 추가 했다고 가정 합니다 OtherInitialize 폼의 메서드에서 Load 메서드 또는 생성자 (호출 후 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

적용 대상