Form.Closing 이벤트

정의

폼이 닫히는 동안 발생합니다.

public:
 event System::ComponentModel::CancelEventHandler ^ Closing;
public event System.ComponentModel.CancelEventHandler Closing;
[System.ComponentModel.Browsable(false)]
public event System.ComponentModel.CancelEventHandler Closing;
[System.ComponentModel.Browsable(false)]
public event System.ComponentModel.CancelEventHandler? Closing;
member this.Closing : System.ComponentModel.CancelEventHandler 
[<System.ComponentModel.Browsable(false)>]
member this.Closing : System.ComponentModel.CancelEventHandler 
Public Custom Event Closing As CancelEventHandler 

이벤트 유형

특성

예제

다음 예제에서는 를 사용하여 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

설명

주의

이벤트는 Closing .NET Framework 2.0부터 사용되지 않습니다. 대신 이벤트를 사용합니다FormClosing.

폼이 Closing 닫히는 동안 이벤트가 발생합니다. 폼을 닫으면 개체 내에서 만든 모든 리소스가 해제되고 양식이 삭제됩니다. 이 이벤트를 취소하면 양식이 열린 상태로 유지됩니다. 폼의 닫기를 취소하려면 이벤트 처리기에 true전달된 의 CancelEventArgs 속성을 로 설정합니다Cancel.

폼이 모달 대화 상자로 표시되면 닫기 단추(폼의 오른쪽 위 모서리에 X가 있는 단추)를 클릭하면 양식이 숨겨지고 속성이 DialogResultDialogResult.Cancel설정됩니다. 사용자가 폼의 이벤트에 대한 이벤트 처리기에서 속성을 설정 DialogResult 하여 닫기 단추를 클릭할 때 속성에 Closing 할당된 DialogResult 값을 재정의할 수 있습니다.

참고

Close 모덜리스 창으로 표시된 에서 메서드를 Form 호출하면 폼의 리소스가 이미 릴리스되었으므로 메서드를 호출 Show 하여 폼을 표시할 수 없습니다. 폼을 숨기고 표시하려면 메서드를 Control.Hide 사용합니다.

주의

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

양식이 MDI 부모 양식인 경우 MDI 부모 폼 Closing 의 이벤트가 발생하기 전에 모든 MDI 자식 폼의 Closing 이벤트가 발생합니다. 또한 Closed MDI 부모 양식의 이벤트가 발생하기 전에 Closed 모든 MDI 자식 양식의 이벤트가 발생합니다. Closing MDI 자식 양식의 이벤트를 취소해도 MDI 부모 양식의 이벤트가 발생하지 않습니다Closing. 그러나 이벤트를 취소하면 부모 양식에 trueCancel 매개 변수로 전달되는 의 CancelEventArgs 속성으로 설정됩니다. 모든 MDI 부모 및 자식 폼을 강제로 닫도록 하려면 MDI 부모 양식에서 속성을 false 로 설정합니다Cancel.

이벤트 처리에 대한 자세한 내용은 이벤트 처리 및 발생 을 참조하십시오.

적용 대상

추가 정보