CancelEventArgs.Cancel プロパティ
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
イベントをキャンセルするかどうかを示す値を取得または設定します。
public:
property bool Cancel { bool get(); void set(bool value); };
C#
public bool Cancel { get; set; }
member this.Cancel : bool with get, set
Public Property Cancel As Boolean
イベントを取り消す場合は true
。それ以外の場合は false
。
次の例では、 と をCancelEventHandler使用CancelEventArgsして のイベントをFormFormClosing処理します。 このコードでは、 という名前isDataSaved
のクラス レベルBooleanの変数を使用して を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." );
}
}
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.");
}
}
' 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
製品 | バージョン |
---|---|
.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 |
GitHub で Microsoft と共同作業する
このコンテンツのソースは GitHub にあります。そこで、issue や pull request を作成および確認することもできます。 詳細については、共同作成者ガイドを参照してください。
.NET に関するフィードバック
.NET はオープンソース プロジェクトです。 フィードバックを提供するにはリンクを選択します。