Form.OnClosing(CancelEventArgs) Metoda

Definice

Closing Vyvolá událost.

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)

Parametry

e
CancelEventArgs

A CancelEventArgs , který obsahuje data události.

Příklady

Následující příklad používá Closing k testování, zda se text v objektu TextBox změnil. Pokud ano, zobrazí se uživateli dotaz, jestli má uložit změny do souboru.

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

Poznámky

Upozornění

Metoda OnClosing je zastaralá počínaje rozhraním .NET Framework 2.0; místo toho použijte metodu OnFormClosing .

Vyvolání události spustí obslužnou rutinu události prostřednictvím delegáta. Další informace najdete v tématu Zpracování a vyvolávání událostí.

Metoda OnClosing také umožňuje odvozeným třídám zpracovat událost bez připojení delegáta. Přepsání této metody je upřednostňovaná technika pro zpracování události v odvozené třídě.

Upozornění

Metody OnClosed a OnClosing se nevolají, když Application.Exit je volána metoda pro ukončení aplikace. Pokud máte ověřovací kód v některé z těchto metod, které musí být provedeny, měli byste volat metodu Form.Close pro každý otevřený formulář zvlášť před voláním Exit metody.

Poznámky pro dědice

Při přepsání OnClosing(CancelEventArgs) v odvozené třídě nezapomeňte volat metodu základní třídy OnClosing(CancelEventArgs) , aby registrovaní delegáti obdrželi událost.

Platí pro

Viz také