Form.Closing イベント

定義

フォームが閉じている間に発生します。

public:
 event System::ComponentModel::CancelEventHandler ^ Closing;
public event System.ComponentModel.CancelEventHandler Closing;
[System.ComponentModel.Browsable(false)]
public event System.ComponentModel.CancelEventHandler Closing;
[System.ComponentModel.Browsable(false)]
public event System.ComponentModel.CancelEventHandler? Closing;
member this.Closing : System.ComponentModel.CancelEventHandler 
[<System.ComponentModel.Browsable(false)>]
member this.Closing : System.ComponentModel.CancelEventHandler 
Public Custom Event Closing As CancelEventHandler 

イベントの種類

属性

次の例では、 を使用 Closing して、 内 TextBox のテキストが変更されたかどうかをテストします。 ある場合、ユーザーは変更をファイルに保存するかどうかを確認するメッセージが表示されます。

private:
   void Form1_Closing( Object^ /*sender*/, System::ComponentModel::CancelEventArgs^ e )
   {
      // Determine if text has changed in the textbox by comparing to original text.
      if ( textBox1->Text != strMyOriginalText )
      {
         // Display a MsgBox asking the user to save changes or abort.
         if ( MessageBox::Show( "Do you want to save changes to your text?", "My Application", MessageBoxButtons::YesNo ) == ::DialogResult::Yes )
         {
            // Cancel the Closing event from closing the form.
            e->Cancel = true;

            // Call method to save file...
         }
      }
   }
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
   // Determine if text has changed in the textbox by comparing to original text.
   if (textBox1.Text != strMyOriginalText)
   {
      // Display a MsgBox asking the user to save changes or abort.
      if(MessageBox.Show("Do you want to save changes to your text?", "My Application",
         MessageBoxButtons.YesNo) ==  DialogResult.Yes)
      {
         // Cancel the Closing event from closing the form.
         e.Cancel = true;
         // Call method to save file...
      }
   }
}
   Private Sub Form1_Closing(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
      ' Determine if text has changed in the textbox by comparing to original text.
      If textBox1.Text <> strMyOriginalText Then
         ' Display a MsgBox asking the user to save changes or abort.
         If MessageBox.Show("Do you want to save changes to your text?", "My Application", MessageBoxButtons.YesNo) = DialogResult.Yes Then
            ' Cancel the Closing event from closing the form.
            e.Cancel = True
         End If ' Call method to save file...
      End If
   End Sub
End Class

注釈

注意事項

イベントはClosing、.NET Framework 2.0 以降では廃止されています。代わりに、 イベントをFormClosing使用してください。

イベントは Closing 、フォームが閉じられている間に発生します。 フォームを閉じると、オブジェクト内で作成されたすべてのリソースが解放され、フォームが破棄されます。 このイベントを取り消した場合、フォームは開いたままです。 フォームのクロージャを取り消すには、イベント ハンドラーに Cancel 渡された の CancelEventArgs プロパティを に true設定します。

フォームがモーダル ダイアログ ボックスとして表示されている場合、[ 閉じる ] ボタン (フォームの右上隅に X が表示されているボタン) をクリックすると、フォームが非表示になり、プロパティが DialogResultDialogResult.Cancel設定されます。 ユーザーが [閉じる] ボタンをクリックしたときにプロパティにDialogResult割り当てられた値をオーバーライドするには、フォームのイベントのイベント ハンドラーで プロパティをClosing設定DialogResultします。

注意

Closeモードレス ウィンドウとして表示される でFormメソッドが呼び出されると、フォームのリソースが既に解放されているため、フォームを表示するメソッドを呼び出Showすことはできません。 フォームを非表示にしてから表示するには、 メソッドを Control.Hide 使用します。

注意事項

Form.Closedおよび Form.Closing イベントは、アプリケーションを終了するために メソッドがApplication.Exit呼び出されたときに発生しません。 実行する必要があるこれらのイベントのいずれかに検証コードがある場合は、 メソッドを Form.Close 呼び出す前に、開いているフォームごとに メソッドを Exit 個別に呼び出す必要があります。

フォームが MDI 親フォームの場合、 Closing MDI 親フォームのイベントが発生する前に、すべての MDI 子フォームの Closing イベントが発生します。 さらに、 Closed MDI 親フォームのイベントが発生する前に、 Closed すべての MDI 子フォームのイベントが発生します。 Closing MDI 子フォームのイベントを取り消しても、MDI 親フォームのイベントが発生することはありませんClosing。 ただし、イベントを取り消すと、親フォームにtrueCancelパラメーターとして渡される の CancelEventArgs プロパティに設定されます。 すべての MDI 親フォームと子フォームを強制的に閉じるには、MDI 親フォームで プロパティを にfalse設定Cancelします。

イベントの処理の詳細については、「処理とイベントの発生」を参照してください。

適用対象

こちらもご覧ください