Share via


Form.OnClosing(CancelEventArgs) Metode

Definisi

Memunculkan kejadian Closing.

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)

Parameter

e
CancelEventArgs

CancelEventArgs yang berisi data peristiwa.

Contoh

Contoh berikut menggunakan Closing untuk menguji apakah teks dalam telah TextBox berubah. Jika sudah, pengguna akan ditanya apakah akan menyimpan perubahan ke 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

Keterangan

Perhatian

Metode OnClosing ini usang dimulai dengan .NET Framework 2.0; gunakan metode sebagai gantinyaOnFormClosing.

Menaikkan peristiwa memanggil penanganan aktivitas melalui delegasi. Untuk informasi selengkapnya, lihat Menangani dan Menaikkan Peristiwa.

Metode ini OnClosing juga memungkinkan kelas turunan untuk menangani peristiwa tanpa melampirkan delegasi. Mengesampingkan metode ini adalah teknik yang disukai untuk menangani peristiwa di kelas turunan.

Perhatian

Metode OnClosed dan OnClosing tidak dipanggil ketika Application.Exit metode dipanggil untuk keluar dari aplikasi Anda. Jika Anda memiliki kode validasi di salah satu metode ini yang harus dijalankan, Anda harus memanggil Form.Close metode untuk setiap formulir terbuka satu per satu sebelum memanggil Exit metode .

Catatan Bagi Inheritor

Saat mengambil alih di kelas turunan OnClosing(CancelEventArgs) , pastikan untuk memanggil metode kelas OnClosing(CancelEventArgs) dasar sehingga delegasi terdaftar menerima peristiwa tersebut.

Berlaku untuk

Lihat juga