Dieser Artikel wurde maschinell übersetzt. Wenn Sie die englische Version des Artikels anzeigen möchten, aktivieren Sie das Kontrollkästchen Englisch. Sie können den englischen Text auch in einem Popupfenster anzeigen, indem Sie den Mauszeiger über den Text bewegen.
Übersetzung
Englisch

Form.Closing-Ereignis

 

Veröffentlicht: Oktober 2016

Tritt beim Schließen des Formulars ein.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

[BrowsableAttribute(false)]
public event CancelEventHandler Closing

System_CAPS_cautionAchtung

The E:System.Windows.Forms.Form.Closing event is obsolete in the .NET Framework version 2.0; use the E:System.Windows.Forms.Form.FormClosing event instead.

The E:System.Windows.Forms.Form.Closing event occurs as the form is being closed. When a form is closed, all resources created within the object are released and the form is disposed. If you cancel this event, the form remains opened. To cancel the closure of a form, set the P:System.ComponentModel.CancelEventArgs.Cancel property of the T:System.ComponentModel.CancelEventArgs passed to your event handler to true.

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 P:System.Windows.Forms.Form.DialogResult property to be set to DialogResult.Cancel. You can override the value assigned to the P:System.Windows.Forms.Form.DialogResult property when the user clicks the Close button by setting the P:System.Windows.Forms.Form.DialogResult property in an event handler for the E:System.Windows.Forms.Form.Closing event of the form.

System_CAPS_noteHinweis

When the M:System.Windows.Forms.Form.Close method is called on a T:System.Windows.Forms.Form displayed as a modeless window, you cannot call the M:System.Windows.Forms.Control.Show method to make the form visible, because the form's resources have already been released. To hide a form and then make it visible, use the M:System.Windows.Forms.Control.Hide method.

System_CAPS_cautionAchtung

The E:System.Windows.Forms.Form.Closed and E:System.Windows.Forms.Form.Closing events are not raised when the M:System.Windows.Forms.Application.Exit method is called to exit your application. If you have validation code in either of these events that must be executed, you should call the M:System.Windows.Forms.Form.Close method for each open form individually before calling the M:System.Windows.Forms.Application.Exit method.

If the form is an MDI parent form, the E:System.Windows.Forms.Form.Closing events of all MDI child forms are raised before the MDI parent form's E:System.Windows.Forms.Form.Closing event is raised. In addition, the E:System.Windows.Forms.Form.Closed events of all MDI child forms are raised before the E:System.Windows.Forms.Form.Closed event of the MDI parent form is raised. Canceling the E:System.Windows.Forms.Form.Closing event of an MDI child form does not prevent the E:System.Windows.Forms.Form.Closing event of the MDI parent form from being raised. However, canceling the event will set to true the P:System.ComponentModel.CancelEventArgs.Cancel property of the T:System.ComponentModel.CancelEventArgs that is passed as a parameter to the parent form. To force all MDI parent and child forms to close, set the P:System.ComponentModel.CancelEventArgs.Cancel property to false in the MDI parent form.

For more information about handling events, see Handling and Raising Events.

The following code example uses E:System.Windows.Forms.Form.Closing to test if the text in a T:System.Windows.Forms.TextBox has changed. 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...
      }
   }
}

.NET Framework
Verfügbar seit 1.1
Zurück zum Anfang
Anzeigen: