Form.Closing イベント
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

public: event CancelEventHandler^ Closing { void add (CancelEventHandler^ value); void remove (CancelEventHandler^ value); }

![]() |
---|
Application.Exit メソッドが呼び出されてアプリケーションを終了する場合、Form.Closed イベントと Form.Closing イベントは発生しません。これらのイベントのいずれかに実行する必要がある検証コードがある場合は、Exit メソッドを呼び出す前に、開いている各フォームに対して Form.Close メソッドを個別に呼び出す必要があります。 |
フォームが MDI 親フォームの場合は、MDI 親フォームの Closing イベントが発生する前に、すべての MDI 子フォームの Closing イベントが発生します。さらに、MDI 親フォームの Closed イベントが発生する前に、すべての MDI 子フォームの Closed イベントが発生します。MDI 子フォームの Closing イベントをキャンセルしても、MDI 親フォームの Closing イベントは発生します。ただし、このイベントをキャンセルすると、親フォームにパラメータとして渡される CancelEventArgs の Cancel プロパティは true に設定されます。MDI 親フォームおよび MDI 子フォームのすべてを強制的に閉じるには、MDI 親フォームの Cancel プロパティを true に設定します。

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 'Form1_Closing End Class 'Form1
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 void Form1Closing(Object sender, System.ComponentModel.CancelEventArgs e) { // Determine if text has changed in the textbox by comparing to // original text. if (textBox1.get_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).Equals( get_DialogResult().Yes)) { // Cancel the Closing event from closing the form. e.set_Cancel(true); // Call method to save file... } } } //Form1Closing

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- Form.Closing イベントのページへのリンク