7

I'm creating a little WPF app in VS2013Express and I've come across a little problem. You see, there are three windows, MainWindow, LatAndLongDialog, TimeCitiesDialog.

`LatAndLongDialog` and `TimeCitiesDialog` are opened from MainWindow (with the click of a button). I want all the other windows to close when the `Closed()` event is called on `MainWindow`. The code on MainWindow.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace GlobalTime_ELITE_for_WPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            UserDescText.Content =
            "Select a TimeCity or enter the latitude and longitude in \n" +
            "to view the World Time there. Or, select another one of the\n" +
            "options below to do that. Go to Help by clicking on the link\n" +
            "on the upper-right corner of the window to view everything you\n" +
            "can do.";
            this.Closed += CloseOff;
        }
        private void OpenTimeCitiesDialog(Object Sender, EventArgs E)
        {
            TimeCitiesDialog ObjectReference = new TimeCitiesDialog();
            ObjectReference.Show();
        }
        private void OpenLatAndLongDialog(Object Sender, EventArgs E)
        {
            LatAndLongDialog ObjectReference = new LatAndLongDialog();
            ObjectReference.Show();
        }
        private void CloseOff(Object Sender, EventArgs E)
        {
            this.Close();
            TimeCitiesDialog tcdor = new TimeCitiesDialog();
            LatAndLongDialog laldor = new LatAndLongDialog();
        }
    }
}

How can I close them all? Please help!

Toni
  • 1,555
  • 4
  • 15
  • 23
  • Why are you instantiating new `TimeCitiesDialog` and `LatAndLongDialog` inside `CloseOff()`? –  May 06 '16 at 04:26

4 Answers4

16

The proper way to shutdown a WPF app is to use Application.Current.Shutdown() . This will close all open Windows, raise some events so that cleanup code can be run, and it can't be canceled. Environment.Exit() terminates the application immediately even if other threads are executing.

You should also consider setting the Owner on non-main Windows. The behavior will likely be more like what you would expect in regards to Z-order, minimizing, and maximizing. As an added bonus, the owned windows will automatically close when the owner Window closes.

James Durda
  • 625
  • 6
  • 12
4
private void CloseAllWindows()
{
    for (int intCounter = App.Current.Windows.Count - 1; intCounter >= 0; intCounter--)
    {
        App.Current.Windows[intCounter].Close();
    }   
}

Close all opened current windows.

Diego Montania
  • 322
  • 5
  • 12
user2960398
  • 363
  • 4
  • 19
3

Use this instead this.Close()

Environment.Exit(0);

this will force everything to close

Mostafiz
  • 7,243
  • 3
  • 28
  • 42
  • please explain it a li'l more – Aravind Suresh Thakidayil May 06 '16 at 03:52
  • While this will close every window in the application, the OP did not say they wanted to exit the application. This will exit the entire app. OP, think about the situation where this isn't the start up form and you just want the logic to close a form from another form. You must keep a reference outside of the scope of the method that creates or shows it. Take a look at my answer for that technique. – Mikanikal May 06 '16 at 04:01
  • 1
    @Mikanikal Incorrect. Considering that `MainWindow` is already in the process of closing and OP wants to close all other windows, Windows will close the app because no other windows for the current process remain open. So the net result is that **the OP wants the program to terminate** –  May 06 '16 at 04:12
  • yes OP wants everything should be close, means program terminate – Mostafiz May 06 '16 at 04:15
  • 1
    `Environment.Exit` is abrupt and closes down the app immediately. `Application.Current.Shutdown()` shutsdown in an orderly fashion. – James Durda May 06 '16 at 04:35
1

If you keep track of the Dialogs outside of the scope of the methods you use to open them, you can call which ever methods on those Dialogs you wish from anywhere within the Class. Here I have them as Class variables and they are instantiated there but not shown until you press the buttons. You can also create "Close" buttons for those specific windows and call their .Close() methods when ever you wish. That will allow you to open and close them at will. You can also call their .Close() methods when the main form closes.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace GlobalTime_ELITE_for_WPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        TimeCitiesDialog tcDialog = new TimeCitiesDialog();
        LatAndLongDialog lalDialog = new LatAndLongDialog();

        public MainWindow()
        {
            InitializeComponent();    

            UserDescText.Content = "Select a TimeCity or enter the latitude and longitude in \n" +
                                 "to view the World Time there. Or, select another one of the\n" +
                                 "options below to do that. Go to Help by clicking on the link\n" +
                                 "on the upper-right corner of the window to view everything you\n" +
                                 "can do.";
            this.Closed += CloseOff;
        }

        private void OpenTimeCitiesDialog(Object Sender, EventArgs E)
        {                
            tcDialog.Show();
        }

        private void OpenLatAndLongDialog(Object Sender, EventArgs E)
        {                
            lalDialog.Show();
        }

        private void CloseOff(Object Sender, EventArgs E)
        {
            // Close the dialogs first, then allow this method
            // to end which will finish the this.Close() process.
            tcDialog.Close();
            lalDialog.Close();
        }
    }
}
Mikanikal
  • 1,082
  • 8
  • 12