This documentation is archived and is not being maintained.

Form.OnClosing Method

Raises the Closing event.

[Visual Basic]
Protected Overridable Sub OnClosing( _
   ByVal e As CancelEventArgs _
)
[C#]
protected virtual void OnClosing(
 CancelEventArgs e
);
[C++]
protected: virtual void OnClosing(
 CancelEventArgs* e
);
[JScript]
protected function OnClosing(
   e : CancelEventArgs
);

Parameters

e
A CancelEventArgs that contains the event data.

Remarks

Raising an event invokes the event handler through a delegate. For more information, see Raising an Event.

The OnClosing method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.

Notes to Inheritors:  When overriding OnClosing in a derived class, be sure to call the base class's OnClosing method so that registered delegates receive the event.

Example

[Visual Basic] 
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

[C#] 
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...
      }
   }
}

[C++] 
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(S"Do you want to save changes to your text?", S"My Application",
            MessageBoxButtons::YesNo) ==  DialogResult::Yes)
         {
            // Cancel the Closing event from closing the form.
            e->Cancel = true;
            // Call method to save file...
         }
      }
   }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

Form Class | Form Members | System.Windows.Forms Namespace | Closing | CancelEventArgs

Show: