Form.Close 方法

定義

關閉表單。Closes the form.

public:
 void Close();
public void Close ();
member this.Close : unit -> unit
Public Sub Close ()

例外狀況

當控制代碼正在建立時,表單會關閉。The form was closed while a handle was being created.

Activated 設為 WindowState 時,您無法從 Maximized 事件呼叫這個方法。You cannot call this method from the Activated event when WindowState is set to Maximized.

備註

當表單關閉時,在物件中建立的所有資源都會關閉,並且會處置表單。When a form is closed, all resources created within the object are closed and the form is disposed. 您可以藉由處理 Closing 事件,並將做為參數傳遞之 CancelEventArgsCancel 屬性設定為事件處理常式,以防止在執行時間關閉表單。You can prevent the closing of a form at run time by handling the Closing event and setting the Cancel property of the CancelEventArgs passed as a parameter to your event handler. 如果您要關閉的表單是應用程式的啟動表單,您的應用程式就會結束。If the form you are closing is the startup form of your application, your application ends.

當表單不會在 Close 上處置時,這兩個條件是當(1)它是多重文件介面(MDI)應用程式的一部分,而且看不到該表單時。和(2)您已使用 ShowDialog 顯示表單。The two conditions when a form is not disposed on Close is when (1) it is part of a multiple-document interface (MDI) application, and the form is not visible; and (2) you have displayed the form using ShowDialog. 在這些情況下,您將需要手動呼叫 Dispose,以標記所有表單的控制項進行垃圾收集。In these cases, you will need to call Dispose manually to mark all of the form's controls for garbage collection.

注意

在顯示為非強制回應視窗的 Form 上呼叫 Close 方法時,您無法呼叫 Show 方法,讓表單可見,因為表單的資源已經發行。When the Close method is called on a Form displayed as a modeless window, you cannot call the Show method to make the form visible, because the form's resources have already been released. 若要隱藏表單,然後讓它顯示,請使用 Control.Hide 方法。To hide a form and then make it visible, use the Control.Hide method.

警告

.NET Framework.NET Framework 2.0 之前,呼叫 Application.Exit 方法來結束應用程式時,不會引發 Form.ClosedForm.Closing 事件。Prior to the .NET Framework.NET Framework 2.0, the Form.Closed and Form.Closing events are not raised when the Application.Exit method is called to exit your application. 如果您在其中一個必須執行的事件中有驗證碼,您應該在呼叫 Exit 方法之前,分別針對每個開啟的表單呼叫 Form.Close 方法。If you have validation code in either of these events that must be executed, you should call the Form.Close method for each open form individually before calling the Exit method.

適用於

另請參閱