Form.FormClosing 이벤트

정의

폼이 닫히기 전에 발생합니다.

public:
 event System::Windows::Forms::FormClosingEventHandler ^ FormClosing;
public event System.Windows.Forms.FormClosingEventHandler FormClosing;
public event System.Windows.Forms.FormClosingEventHandler? FormClosing;
member this.FormClosing : System.Windows.Forms.FormClosingEventHandler 
Public Custom Event FormClosing As FormClosingEventHandler 

이벤트 유형

예제

다음 예제에서는 이 멤버의 사용을 보여 줍니다. 예제에서는 이벤트 처리기에서 보고서를 FormClosing 이벤트입니다. 이 보고서를 통해 이벤트 발생 하 고 도움이 될 수 있습니다 디버깅 하는 경우를 학습할 수 있습니다. 자주 발생 하는 이벤트 또는 여러 이벤트를 보고 하려면 바꾸는 것이 좋습니다 MessageBox.Show 사용 하 여 Console.WriteLine 줄에 메시지를 추가 또는 TextBox합니다.

예제 코드를 실행 하려면 형식의 인스턴스를 포함 하는 프로젝트에 붙여넣습니다 Form 라는 Form1합니다. 연결 된 이벤트 처리기는 확인 된 FormClosing 이벤트입니다.

private void Form1_FormClosing(Object sender, FormClosingEventArgs e) {

System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
messageBoxCS.AppendFormat("{0} = {1}", "CloseReason", e.CloseReason );
messageBoxCS.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "Cancel", e.Cancel );
messageBoxCS.AppendLine();
MessageBox.Show(messageBoxCS.ToString(), "FormClosing Event" );
}
Private Sub Form1_FormClosing(sender as Object, e as FormClosingEventArgs) _ 
     Handles Form1.FormClosing

    Dim messageBoxVB as New System.Text.StringBuilder()
    messageBoxVB.AppendFormat("{0} = {1}", "CloseReason", e.CloseReason)
    messageBoxVB.AppendLine()
    messageBoxVB.AppendFormat("{0} = {1}", "Cancel", e.Cancel)
    messageBoxVB.AppendLine()
    MessageBox.Show(messageBoxVB.ToString(),"FormClosing Event")

End Sub

설명

폼이 FormClosing 닫히는 동안 이벤트가 발생합니다. 폼이 닫혀 있으면 삭제되어 양식과 연결된 모든 리소스가 해제됩니다. 이 이벤트를 취소하면 양식이 열린 상태로 유지됩니다. 폼의 닫기를 취소하려면 이벤트 처리기에 true전달된 의 FormClosingEventArgs 속성을 로 설정합니다Cancel.

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

참고

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

양식이 MDI(다중 문서 인터페이스) 부모 양식 FormClosing 인 경우 MDI 부모 폼의 이벤트가 발생하기 전에 모든 MDI 자식 폼의 FormClosing 이벤트가 발생합니다. 마찬가지로 MDI FormClosed 부모 양식의 이벤트가 발생하기 전에 FormClosed 모든 MDI 자식 폼의 이벤트가 발생합니다. FormClosing MDI 자식 양식의 이벤트를 취소해도 MDI 부모 양식의 이벤트가 발생하지 않습니다FormClosing. 그러나 이벤트를 취소하면 부모 양식에 trueCancel 매개 변수로 전달되는 클래스의 FormClosingEventArgs 속성으로 설정됩니다. 모든 MDI 부모 및 자식 폼을 강제로 닫도록 하려면 MDI 부모 양식에서 속성을 false 로 설정합니다Cancel.

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

적용 대상

추가 정보