Form.Close Form.Close Form.Close Form.Close Method

定义

关闭窗体。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事件并Cancel将作为参数传递的的CancelEventArgs属性设置为事件处理程序,可以防止在运行时关闭窗体。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显示为无模式窗口的上调用Show 方法时,不能调用方法使窗体可见,因为窗体的资源已被释放。CloseWhen 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.

注意

在2.0 之前Form.Closed ,调用Application.Exit方法退出Form.Closing应用程序时不会引发和事件。 .NET Framework.NET FrameworkPrior 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. 如果在必须执行的任何一个事件中都有验证代码,则应在Form.Close Exit调用方法之前为每个打开的窗体分别调用方法。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.

适用于

另请参阅