Form.OnClosing(CancelEventArgs) Método

Definición

Genera el evento Closing.Raises the Closing event.

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)

Parámetros

e
CancelEventArgs

Objeto CancelEventArgs que contiene los datos del evento.A CancelEventArgs that contains the event data.

Ejemplos

En el ejemplo siguiente se usa Closing para probar si el texto de una TextBox ha cambiado.The following example uses Closing to test if the text in a TextBox has changed. Si es así, se pregunta al usuario si desea guardar los cambios en un archivo.If it has, the user is asked whether to save the changes to a 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 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

Comentarios

Precaución

El método OnClosing está obsoleto a partir del .NET Framework 2,0; en su lugar, use el método OnFormClosing.The OnClosing method is obsolete starting with the .NET Framework 2.0; use the OnFormClosing method instead.

Cuando se genera un evento, se invoca el controlador de eventos a través de un delegado.Raising an event invokes the event handler through a delegate. Para obtener más información, consulte controlar y provocar eventos.For more information, see Handling and Raising Events.

El método OnClosing también permite que las clases derivadas controlen el evento sin adjuntar ningún delegado.The OnClosing method also allows derived classes to handle the event without attaching a delegate. Reemplazar este método es la técnica preferida para controlar el evento en una clase derivada.Overriding this method is the preferred technique for handling the event in a derived class.

Precaución

No se llama a los métodos OnClosed y OnClosing cuando se llama al método Application.Exit para salir de la aplicación.The OnClosed and OnClosing methods are not called when the Application.Exit method is called to exit your application. Si tiene código de validación en cualquiera de estos métodos que se debe ejecutar, debe llamar al método Form.Close para cada formulario abierto individualmente antes de llamar al método Exit.If you have validation code in either of these methods that must be executed, you should call the Form.Close method for each open form individually before calling the Exit method.

Notas a los desarrolladores de herederos

Al reemplazar OnClosing(CancelEventArgs) en una clase derivada, asegúrese de llamar al método OnClosing(CancelEventArgs) de la clase base para que los delegados registrados reciban el evento.When overriding OnClosing(CancelEventArgs) in a derived class, be sure to call the base class's OnClosing(CancelEventArgs) method so that registered delegates receive the event.

Se aplica a

Consulte también: