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

備註

警告

從 .NET Framework 2.0 開始,事件 Closing 已過時;請改用 FormClosing 事件。

當表單正在關閉時,就會發生此 Closing 事件。 關閉表單時,會釋放物件內建立的所有資源,並處置表單。 如果您取消此事件,表單會保持開啟狀態。 若要取消表單的關閉,請將 Cancel 傳遞給事件處理常式的 CancelEventArgs 屬性設定為 true

當表單顯示為強制回應對話方塊時,按一下 [ 關閉 ] 按鈕 (表單右上角的 X 按鈕) 會導致表單隱藏,並將 DialogResult 屬性設定為 DialogResult.Cancel 。 當使用者按一下 [關閉] 按鈕時,您可以在表單事件的事件處理常式 Closing 中設定 DialogResult 屬性,以覆寫指派給 DialogResult 屬性的值。

注意

Close在顯示為無強制回應視窗的 上 Form 呼叫 方法時,您無法呼叫 Show 方法讓表單可見,因為表單的資源已經釋放。 若要隱藏表單,然後使其可見,請使用 Control.Hide 方法。

警告

Form.Closed呼叫 方法以結束您的應用程式時, Application.Exit 不會引發 和 Form.Closing 事件。 如果您在其中一個必須執行的事件中有驗證程式代碼,您應該先個別呼叫 Form.Close 每個開啟表單的 方法,再呼叫 Exit 方法。

如果表單是 MDI 父表單, Closing 則會在引發 MDI 父表單的事件之前引發所有 MDI 子表單 Closing 的事件。 此外,所有 MDI 子表單的事件都會 Closed 在引發 MDI 父表單的事件之前 Closed 引發。 取消 Closing MDI 子表單的事件並不會防止 Closing 引發 MDI 父表單的事件。 不過,取消事件將會設定 trueCancel 為 將 做為參數傳遞至父表單之 的 CancelEventArgs 屬性。 若要強制關閉所有 MDI 父表單和子表單,請將 Cancel MDI 父表單中的 屬性設定為 false

如需處理事件的詳細資訊,請參閱 處理和引發事件

適用於

另請參閱