Window.Closing 이벤트

정의

Close()가 호출된 직후에 발생하며 창 닫기를 취소하도록 처리할 수 있습니다.

public:
 event System::ComponentModel::CancelEventHandler ^ Closing;
public event System.ComponentModel.CancelEventHandler Closing;
member this.Closing : System.ComponentModel.CancelEventHandler 
Public Custom Event Closing As CancelEventHandler 

이벤트 유형

예외

창이 닫히는 동안 Visibility, Show() 또는 ShowDialog()가 호출되거나 Close()가 설정된 경우

예제

다음 예제에서는 Window 닫기 위해 사용자 개입이 필요한지 여부를 결정하는 을 보여 줍니다.

<Window 
  x:Class="CSharp.DataWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Closing="DataWindow_Closing"  
    >
  <Grid>
    <TextBox Name="documentTextBox" AcceptsReturn="True" AcceptsTab="True" TextChanged="documentTextBox_TextChanged"></TextBox>
  </Grid>
</Window>
using System; // EventArgs
using System.ComponentModel; // CancelEventArgs
using System.Windows; // window

namespace CSharp
{
    public partial class DataWindow : Window
    {
        // Is data dirty
        bool isDataDirty = false;

        public DataWindow()
        {
            InitializeComponent();
        }

        void documentTextBox_TextChanged(object sender, EventArgs e)
        {
            this.isDataDirty = true;
        }

        void DataWindow_Closing(object sender, CancelEventArgs e)
        {
            MessageBox.Show("Closing called");

            // If data is dirty, notify user and ask for a response
            if (this.isDataDirty)
            {
                string msg = "Data is dirty. Close without saving?";
                MessageBoxResult result = 
                  MessageBox.Show(
                    msg, 
                    "Data App", 
                    MessageBoxButton.YesNo, 
                    MessageBoxImage.Warning);
                if (result == MessageBoxResult.No)
                {
                    // If user doesn't want to close, cancel closure
                    e.Cancel = true;
                }
            }
        }
    }
}
Imports System ' EventArgs
Imports System.ComponentModel ' CancelEventArgs
Imports System.Windows ' window

Namespace VisualBasic
    Partial Public Class DataWindow
        Inherits Window
        ' Is data dirty
        Private isDataDirty As Boolean = False

        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub documentTextBox_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
            Me.isDataDirty = True
        End Sub

        Private Sub DataWindow_Closing(ByVal sender As Object, ByVal e As CancelEventArgs)
            MessageBox.Show("Closing called")

            ' If data is dirty, notify user and ask for a response
            If Me.isDataDirty Then
                Dim msg As String = "Data is dirty. Close without saving?"
                Dim result As MessageBoxResult = MessageBox.Show(msg, "Data App", MessageBoxButton.YesNo, MessageBoxImage.Warning)
                If result = MessageBoxResult.No Then
                    ' If user doesn't want to close, cancel closure
                    e.Cancel = True
                End If
            End If
        End Sub
    End Class
End Namespace

설명

Closing 는 창을 닫는 시기를 감지하기 위해 처리할 수 있습니다(예: 가 호출된 경우 Close ). 또한 을 Closing 사용하여 창이 닫히지 않도록 할 수 있습니다. 창이 닫히지 않도록 하려면 인수trueCancelEventArgs 속성을 로 설정할 Cancel 수 있습니다.

Closing 호출되거나 Close , 창의 닫기 단추를 클릭하거나, 사용자가 Alt+F4를 누르면 이벤트가 발생합니다.

를 사용하여 Show소유자 창에서 소유 창을 열었고 소유자 창이 닫혀 있으면 소유 창의 Closing 이벤트가 발생하지 않습니다. 창의 소유자가 닫힌 경우(참조 Owner) Closing 소유 창에서 가 발생하지 않습니다.

가 호출되면 Shutdown 각 창에 Closing 대한 이벤트가 발생합니다. 그러나 가 Closing 취소되면 취소는 무시됩니다.

사용자가 로그 오프 하거나 종료 하기 때문에 세션을 종료 하는 경우 Closing 발생 하지 않으면 처리 SessionEnding 애플리케이션을 닫지는 코드를 구현 합니다.

애플리케이션의 수명 동안 여러 번 창 숨기기 및 표시 하 고 창 될 때마다 다시 인스턴스화하는 데 않을 경우 표시할 때마다 처리할 수 있습니다 합니다 Closing 이벤트를 취소 하 고 호출 된 Hide 메서드. 그런 다음 동일한 instance 호출 Show 하여 다시 열 수 있습니다.

적용 대상

추가 정보