Form.OnClosing(CancelEventArgs) 方法  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
引发 Closing 事件。
protected:
 virtual void OnClosing(System::ComponentModel::CancelEventArgs ^ e);
	protected virtual void OnClosing(System.ComponentModel.CancelEventArgs e);
	abstract member OnClosing : System.ComponentModel.CancelEventArgs -> unit
override this.OnClosing : System.ComponentModel.CancelEventArgs -> unit
	Protected Overridable Sub OnClosing (e As CancelEventArgs)
	参数
包含事件数据的 CancelEventArgs。
示例
以下示例使用 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
	注解
注意
方法OnClosing从 .NET Framework 2.0 开始已过时;请改用 OnFormClosing 方法。
引发事件时,将通过委托调用事件处理程序。 有关详细信息,请参阅 处理和引发事件。
OnClosing 方法还允许派生类对事件进行处理而不必附加委托。 重写此方法是在派生类中处理事件的首选方法。
注意
OnClosing调用 OnClosed 和 方法退出应用程序时Application.Exit,不会调用 和 方法。 如果其中任一方法中都有必须执行的验证代码,则应在调用 方法之前单独为每个打开的窗体调用 Form.CloseExit 方法。
继承者说明
在派生类中重写 OnClosing(CancelEventArgs) 时,一定要调用基类的 OnClosing(CancelEventArgs) 方法,以便已注册的委托对事件进行接收。