Form.OnClosing(CancelEventArgs) Yöntem

Tanım

Olayı tetikler 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)

Parametreler

e
CancelEventArgs

CancelEventArgs Olay verilerini içeren bir.

Örnekler

Aşağıdaki örnek, içindeki TextBox metnin değişip değişmediğini test etmek için kullanırClosing. Varsa, kullanıcıya değişiklikleri bir dosyaya kaydedip kaydetmediği sorulur.

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

Açıklamalar

Dikkat

OnClosing.NET Framework 2.0 ile başlayan yöntem kullanımdan kaldırıldı; bunun yerine yöntemini kullanınOnFormClosing.

Olay bildirmek, bir temsilci yoluyla olay işleyicisini çağırır. Daha fazla bilgi için bkz. Olayları İşleme ve Oluşturma.

yöntemi, OnClosing türetilmiş sınıfların bir temsilci eklemeden olayı işlemesine de izin verir. Bu yöntemi geçersiz kılma, türetilmiş bir sınıftaki olayı işlemek için tercih edilen tekniktir.

Dikkat

Uygulamanızdan OnClosed çıkmak için yöntemi çağrıldığında Application.Exit ve OnClosing yöntemleri çağrılmıyor. Bu yöntemlerden birinde yürütülmesi gereken doğrulama kodunuz varsa, yöntemini çağırmadan önce her açık form için yöntemini ayrı ayrı çağırmanız Form.CloseExit gerekir.

Devralanlara Notlar

Türetilmiş bir sınıfta geçersiz kıldığınızda OnClosing(CancelEventArgs) , kayıtlı temsilcilerin olayı alması için temel sınıfın OnClosing(CancelEventArgs) yöntemini çağırdığınızdan emin olun.

Şunlara uygulanır

Ayrıca bkz.