2

I want to perform a particular action when user cancels my dialog by clicking the close button (red X button)

enter image description here

and not when the form is closing because of some other operation. How i can determine whether the

private void Window_Closing(object sender, CancelEventArgs e)

event is raised by the button ?

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="60" Width="284" WindowStartupLocation="CenterScreen" 
    BorderBrush="#FFCCCCCC"
    BorderThickness="2"
    Background="#FFE0E0E0"
    WindowStyle="SingleBorderWindow" 
    ShowInTaskbar="False" ResizeMode="NoResize" Closing="Window_Closing">
Hemant Bhatt
  • 475
  • 1
  • 6
  • 24
  • Window_Closing will be fired as soon as someone close the window. Let it be by Cross button or by the this.close(). It really does not required to catch it from the close button. What exactly you want to achieve? – Pradip Apr 08 '13 at 11:36
  • @PradipKT i want to know who actually is firing the closing event . Is it the this.close() call the cancel button. – Hemant Bhatt Apr 08 '13 at 12:21
  • :: If you want to do anything explicit at the time of cross button click then you can do it in the OnFormClosing.. But as per MSDN.. When we use this.Close(me.Close- VB) or click on the Cross button both calls OnFormClosing. Do you want an example on how to over ride OnFormClosing ? Please let me know. – Pradip Apr 08 '13 at 12:26
  • override you button clicks (or commands whatever) - so you know it's via form. What are the 'other operations' in your case. You could also remove the 'close' if that's what you want – NSGaga-mostly-inactive Apr 08 '13 at 13:33
  • this.Close() is firing the Closing handler. – Dilshod Apr 08 '13 at 13:34

2 Answers2

0
    public MainWindow()
    {
        InitializeComponent();
        this.Closing+=new System.ComponentModel.CancelEventHandler(MainWindow_Closing);

    }
    private void MainWindow_Closing(object sender, EventArgs e)
    {
        MessageBox.Show("salman");

    }
YOusaFZai
  • 698
  • 5
  • 21
  • i want to say that in MainWindow_Closing function you can perform the task u want!!! if it is not helpful for u kindly elaborate ur query!!! – YOusaFZai Apr 08 '13 at 12:32
0

My question is what are the other ways of closing this window? My understanding is the sender is always going to be the Window.

I would do the following - for all buttons or user based close - set a public property on the Window (something like bool ClosedByUser) and set it to "true" and then call Close().

For other cases (such as closing by clicking "X"), the property is by default set to false. In the closing event handler, use the property to make the decision.

Please note: There are other questions and answers similar to this on StackOverflow.

Community
  • 1
  • 1
  • what you suggesting is a hack cause it will cover only some specified scenarios and for that too one 've to maintain equal number of public properties and if the form is getting closed because of application crash then one won't be able to set the public properties how will you get the senders information in unknown scenarios like this ? – Hemant Bhatt Apr 09 '13 at 04:22