Form.Closing Evento

Definición

Se produce cuando se cierra el formulario.Occurs when the form is closing.

public:
 event System::ComponentModel::CancelEventHandler ^ Closing;
[System.ComponentModel.Browsable(false)]
public event System.ComponentModel.CancelEventHandler Closing;
public event System.ComponentModel.CancelEventHandler Closing;
member this.Closing : System.ComponentModel.CancelEventHandler 
Public Custom Event Closing As CancelEventHandler 
Atributos

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 evento Closing está obsoleto a partir del .NET Framework 2,0; en su lugar, utilice el evento FormClosing.The Closing event is obsolete starting with the .NET Framework 2.0; use the FormClosing event instead.

El evento Closing se produce cuando se está cerrando el formulario.The Closing event occurs as the form is being closed. Cuando se cierra un formulario, se liberan todos los recursos creados en el objeto y se desecha el formulario.When a form is closed, all resources created within the object are released and the form is disposed. Si cancela este evento, el formulario permanece abierto.If you cancel this event, the form remains opened. Para cancelar el cierre de un formulario, establezca la propiedad Cancel de la CancelEventArgs que se pasa al controlador de eventos a true.To cancel the closure of a form, set the Cancel property of the CancelEventArgs passed to your event handler to true.

Cuando un formulario se muestra como un cuadro de diálogo modal, al hacer clic en el botón cerrar (el botón con una X en la esquina superior derecha del formulario), se oculta el formulario y se establece la propiedad DialogResult en DialogResult.Cancel.When a form is displayed as a modal dialog box, clicking the Close button (the button with an X at the upper-right corner of the form) causes the form to be hidden and the DialogResult property to be set to DialogResult.Cancel. Puede invalidar el valor asignado a la propiedad DialogResult cuando el usuario hace clic en el botón cerrar estableciendo la propiedad DialogResult en un controlador de eventos para el evento Closing del formulario.You can override the value assigned to the DialogResult property when the user clicks the Close button by setting the DialogResult property in an event handler for the Closing event of the form.

Nota

Cuando se llama al método Close en un Form que se muestra como una ventana no modal, no se puede llamar al método Show para que el formulario sea visible, ya que los recursos del formulario ya se han liberado.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. Para ocultar un formulario y, a continuación, hacer que esté visible, use el método Control.Hide.To hide a form and then make it visible, use the Control.Hide method.

Precaución

Los eventos Form.Closed y Form.Closing no se generan cuando se llama al método Application.Exit para salir de la aplicación.The Form.Closed and Form.Closing events are not raised when the Application.Exit method is called to exit your application. Si tiene código de validación en cualquiera de estos eventos 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 events that must be executed, you should call the Form.Close method for each open form individually before calling the Exit method.

Si el formulario es un formulario MDI primario, los eventos de Closing de todos los formularios MDI secundarios se generan antes de que se produzca el evento de Closing del formulario MDI primario.If the form is an MDI parent form, the Closing events of all MDI child forms are raised before the MDI parent form's Closing event is raised. Además, los eventos de Closed de todos los formularios MDI secundarios se generan antes de que se produzca el evento de Closed del formulario primario MDI.In addition, the Closed events of all MDI child forms are raised before the Closed event of the MDI parent form is raised. Cancelar el Closing evento de un formulario MDI secundario no impide que se genere el evento de Closing del formulario primario MDI.Canceling the Closing event of an MDI child form does not prevent the Closing event of the MDI parent form from being raised. Sin embargo, si se cancela el evento, se establecerá en true la propiedad Cancel de la CancelEventArgs que se pasa como un parámetro al formulario primario.However, canceling the event will set to true the Cancel property of the CancelEventArgs that is passed as a parameter to the parent form. Para forzar que todos los formularios MDI primarios y secundarios se cierren, establezca la propiedad Cancel en false en el formulario primario MDI.To force all MDI parent and child forms to close, set the Cancel property to false in the MDI parent form.

Para obtener más información sobre el manejo de eventos, consulte controlar y provocar eventos.For more information about handling events, see Handling and Raising Events.

Se aplica a

Consulte también: