Form.OnClosing(CancelEventArgs) 메서드

정의

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)

매개 변수

e
CancelEventArgs

이벤트 데이터를 포함하는 CancelEventArgs입니다.

예제

다음 예제에서는 를 사용하여 ClosingTextBox 텍스트가 변경되었는지 테스트합니다. 있는 경우 변경 내용을 파일에 저장할지 여부를 묻는 메시지가 사용자에게 표시됩니다.

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

설명

주의

메서드는 OnClosing .NET Framework 2.0부터 사용되지 않습니다. 대신 메서드를 OnFormClosing 사용합니다.

이벤트가 발생하면 대리자를 통해 이벤트 처리기가 호출됩니다. 자세한 내용은 이벤트 처리 및 발생합니다.

또한 OnClosing 메서드를 사용하면 파생 클래스가 대리자를 연결하지 않고도 이벤트를 처리할 수 있습니다. 이 메서드를 재정의하는 것은 파생 클래스에서 이벤트를 처리하는 데 선호되는 기술입니다.

주의

OnClosed 하 고 OnClosing 메서드 호출 되지 않습니다는 Application.Exit 메서드를 호출 하 여 애플리케이션을 종료 합니다. 실행해야 하는 이러한 메서드 중 하나에 유효성 검사 코드가 있는 경우 메서드를 호출하기 전에 열려 있는 각 양식에 대해 메서드를 Exit 개별적으로 호출 Form.Close 해야 합니다.

상속자 참고

파생 클래스에서 OnClosing(CancelEventArgs)를 재정의하는 경우 등록된 대리자가 이벤트를 받도록 기본 클래스의 OnClosing(CancelEventArgs) 메서드를 호출해야 합니다.

적용 대상

추가 정보