Form.OnClosing-Methode
Löst das Closing-Ereignis aus.
Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)
Syntax
'Declaration
Protected Overridable Sub OnClosing ( _
e As CancelEventArgs _
)
'Usage
Dim e As CancelEventArgs
Me.OnClosing(e)
protected virtual void OnClosing (
CancelEventArgs e
)
protected:
virtual void OnClosing (
CancelEventArgs^ e
)
protected void OnClosing (
CancelEventArgs e
)
protected function OnClosing (
e : CancelEventArgs
)
Parameter
- e
Eine Instanz von CancelEventArgs, die die Ereignisdaten enthält.
Hinweise
Durch das Auslösen eines Ereignisses wird der Ereignishandler über einen Delegaten aufgerufen. Weitere Informationen finden Sie unter Auslösen eines Ereignisses.
Mit der OnClosing-Methode können auch abgeleitete Klassen das Ereignis ohne Anfügen eines Delegaten behandeln. Dies ist das bevorzugte Verfahren für die Behandlung des Ereignisses in einer abgeleiteten Klasse.
Hinweise für Erben Wenn Sie OnClosing in einer abgeleiteten Klasse überschreiben, müssen Sie die OnClosing-Methode der Basisklasse aufrufen, sodass registrierte Delegaten das Ereignis empfangen.
Beispiel
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
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 void Form1Closing(Object sender,
System.ComponentModel.CancelEventArgs e)
{
// Determine if text has changed in the textbox by comparing to
// original text.
if (textBox1.get_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).Equals(
get_DialogResult().Yes)) {
// Cancel the Closing event from closing the form.
e.set_Cancel(true);
// Call method to save file...
}
}
} //Form1Closing
Plattformen
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.
Versionsinformationen
.NET Framework
Unterstützt in: 2.0, 1.1, 1.0
.NET Compact Framework
Unterstützt in: 2.0, 1.0
Siehe auch
Referenz
Form-Klasse
Form-Member
System.Windows.Forms-Namespace
Closing
CancelEventArgs