37

How can I catch the event of the window close button(red X button on window right top corner) in a WPF form? We have got the closing event, window unloaded event also, but we want to show a pop up if he clicks the close button of the WPF form.

1
  • So what happened if you did something in the Closing event, Did you try anything ?
    – V4Vendetta
    Jan 23, 2012 at 10:05

6 Answers 6

49

Use the Closing event in the Window, you can handle it like this to prevent it from closing:

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    e.Cancel = true;
}
2
  • 5
    I needed to all the closing handler to the respective window's XAML: <Window [...] Closing="myWindow_Closing"> Apr 1, 2015 at 8:42
  • 15
    Do remember though, that this event also fires when you call this.Close() from the code behind. Nov 9, 2016 at 8:43
4

SOLUTION:

Have a flag to identify if Close() method is called from other than X icon button. (eg: IsNonCloseButtonClicked;)

Have a conditional statement inside Closing () event method which checks if the IsNonCloseButtonClicked is false.

If false, the app is trying to close itself through other than X icon button. If true, it means X icon button is clicked for closing this app.

[Sample Code]

private void buttonCloseTheApp_Click (object sender, RoutedEventArgs e) {
  IsNonCloseButtonClicked = true;
  this.Close (); // this will trigger the Closing () event method
}


private void MainWindow_Closing (object sender, System.ComponentModel.CancelEventArgs e) {
  if (IsNonCloseButtonClicked) {
    e.Cancel = !IsValidated ();

    // Non X button clicked - statements
    if (e.Cancel) {
      IsNonCloseButtonClicked = false; // reset the flag
      return;
    }
  } else {

    // X button clicked - statements
  }
}
2

if it is pressed confirm button in form2 do action, if it is pressed X-button do nothing:

public class Form2
{
  public bool confirm { get; set; }

    public Form2()
        {
            confirm = false;
            InitializeComponent(); 
        }

   private void Confirm_Button_Click(object sender, RoutedEventArgs e)
    {
       //your code
       confirm = true;
       this.Close();

    }

}

first form:

public void Form2_Closing(object sender, CancelEventArgs e)
        {
            if(Form2.confirm == false) return;

            //your code 
        }
0
0

In VB.NET:

    Private Sub frmMain_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
    ' finalize the class

    End Sub

To disable the Form X button:

'=====================================================
' Disable the X button on the control bar
'=====================================================
Private Const CP_NOCLOSE_BUTTON As Integer = &H200
Protected Overloads Overrides ReadOnly Property CreateParams() As CreateParams
    Get
        Dim myCp As CreateParams = MyBase.CreateParams
        myCp.ClassStyle = myCp.ClassStyle Or CP_NOCLOSE_BUTTON
        Return myCp
    End Get
End Property
0

at form1.Designer.cs put below code to assign the event

this.Closing += Window_Closing;

at form1.cs put the closing function

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    //change the event to avoid close form
    e.Cancel = true;
}
0

Try this:

        protected override void OnClosing(CancelEventArgs e)
        {
            this.Visibility = Visibility.Hidden;

            string msg = "Close or not?";
            MessageBoxResult result =
              MessageBox.Show(
                msg,
                "Warning",
                MessageBoxButton.YesNo,
                MessageBoxImage.Warning);
            if (result == MessageBoxResult.No)
            {
                // If user doesn't want to close, cancel closure
                e.Cancel = true;
            }
            else
            {
                e.Cancel = false;
            }
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.