8

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!

1
  • Why are you instantiating new TimeCitiesDialog and LatAndLongDialog inside CloseOff()? Commented May 6, 2016 at 4:26

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

5
private void CloseAllWindows() { for (int intCounter = App.Current.Windows.Count - 1; intCounter >= 0; intCounter--) { App.Current.Windows[intCounter].Close(); } } 

Close all opened current windows.

Comments

3

Use this instead this.Close()

Environment.Exit(0); 

this will force everything to close

5 Comments

please explain it a li'l more
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 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
yes OP wants everything should be close, means program terminate
Environment.Exit is abrupt and closes down the app immediately. Application.Current.Shutdown() shutsdown in an orderly fashion.
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(); } } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.