通过


CancelEventArgs 类

定义

提供可取消事件的数据。

public ref class CancelEventArgs : EventArgs
public class CancelEventArgs : EventArgs
type CancelEventArgs = class
    inherit EventArgs
Public Class CancelEventArgs
Inherits EventArgs
继承
CancelEventArgs
派生

示例

下面的示例使用 CancelEventArgs 和一个 CancelEventHandler 处理 Closing 事件 Form。 此代码假定你已使用名为 的类级变量创建了一个 。 它还假定已添加一个语句,用于从窗体Load的方法或构造函数(调用InitializeComponent后)调用OtherInitialize该方法。

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
void OtherInitialize()
{
    Closing += Form1_Closing;
    // Exchange commented line and note the difference.
    isDataSaved = true;
    //this.isDataSaved = false;
}

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

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

注解

组件即将执行可取消的操作(例如 Closing 事件 Form)时,组件将引发可取消事件。

注释

事件 Closing 已弃用,已被替换 FormClosing。 它作为示例提供,只是为了说明它的 CancelEventArgs用法。

CancelEventArgs 提供属性 Cancel 以指示是否应取消事件。

构造函数

名称 说明
CancelEventArgs()

使用属性设置为 初始化类的新实例

CancelEventArgs(Boolean)

使用属性设置为给定值初始化类Cancel的新实例CancelEventArgs

属性

名称 说明
Cancel

获取或设置一个值,该值指示是否应取消事件。

方法

名称 说明
Equals(Object)

确定指定的对象是否等于当前对象。

(继承自 Object)
GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
ToString()

返回一个表示当前对象的字符串。

(继承自 Object)

适用于

另请参阅