-1

I designed a WPF Window and when I press ESC another small window opens which asks "Are you sure you want to exit?". When I press Yes I want to close the confirmation window and the main window. How can I do that in WPF C#? I used this method. Mainwindow mainwin=new Mainwindow(); mainwin.Close(); when the yes button is pressed.

  • what is that 'small window', is that a Form that you created? share the relevant code snippets from your project. – Anand Sowmithiran Jun 18 '22 at 07:26
  • See if the answer for this [question](https://stackoverflow.com/questions/52711736/how-can-i-close-a-child-window-when-its-parent-window-is-closed-wpf), meets your need. – Anand Sowmithiran Jun 18 '22 at 07:27
  • Its another WPf window – Anosh Younas Jun 18 '22 at 07:34
  • In the event handler in the confirmation form, you need to have the reference of the main form, so that you can call Close() on it. – Anand Sowmithiran Jun 18 '22 at 07:36
  • Does this answer your question? [Handling the window closing event with WPF / MVVM Light Toolkit](https://stackoverflow.com/questions/3683450/handling-the-window-closing-event-with-wpf-mvvm-light-toolkit). Ignore the _" MVVM Light Toolkit"_ aspect –  Jun 18 '22 at 08:19
  • Please improve your question and post the code that shows how you show the dialog. What exactly is not working when you call `mainwin.Close();`? – BionicCode Jun 18 '22 at 17:22

1 Answers1

0

If you open the window as a dialog, then when you close it emits a confirmation response and then closes.

 Secondview = new Secondview ();

            if (view.ShowDialog() == true)
            {
                this.Close();
            }
Eliezer V.M.
  • 1
  • 1
  • 1