blah blah blah is here! blah blah » Close

up1down
link

I have several child windows that I need to display. To show the dialog I have been:

private void ButtonUsers_Click(object sender, System.Windows.RoutedEventArgs e)
{
//Make main window transparent
this.Opacity=.6;

//Create child window if it doesn't exist
if(wUsers==null)
wUsers = new WindowUsersGroups();

//Show window
wUsers.ShowDialog();

//Make main window opaque again
this.Opacity=1;
}


... and to close th child window I have been doing...

private void ButtonClose_Click(object sender, System.Windows.RoutedEventArgs e)
{
this.Close();
}


This works fine once but then throws an exception the second time around. I thought the window object was destroyed when Close() was called so I figured that wUsers would be null. That's obviously not the case.

I also tried using creating an instance of all child windows in the constructor of my main window then simply...

private void ButtonUsers_Click(object sender, System.Windows.RoutedEventArgs e)
{
//Make main window transparent
this.Opacity=.6;

//Show window
wUsers.ShowDialog();

//Make main window opaque again
this.Opacity=1;
}


private void ButtonClose_Click(object sender, System.Windows.RoutedEventArgs e)
{
this.Hide();
}


This works but for some reason it leaves the process alive even after I close the main window. I have to go into the task manager and kill it every time.

So, how should I be doing this?

last answered 7 months ago

1 answers

link

Confusingly, there are a number of basic differences between the workings of WPF and Windows Forms (WF).

In WF, if you open a form with ShowDialog(), then calling the Close() method doesn't actually close it - it just hides it.

In WPF, if you open a window with ShowDialog(), then calling the Close() method does close it.

In WF, the default setting is that, if you close the main form, the whole application closes.

In WPF, the default setting is that the application doesn't close until all windows have been closed.

Consequently, your first piece of code (using this.Close()) could be made to work by creating the child window anew on each pressing of the button:

//Create new child window each time and show it modally           
wUsers = new WindowUsersGroups();
wUsers.ShowDialog();


Your second piece of code (using this.Hide()) could be made to work by creating the child window if it hasn't yet been created in the button click eventhandler rather than the main window's constructor:
//Create child window if it doesn't exist and show it modally:          
if(wUsers==null)
wUsers = new WindowUsersGroups();
wUsers.ShowDialog();

and, in addition, by setting the Application's ShutdownMode property in the app.xaml file to ShutdownMode.OnMainWindowClose which is an enum value. For example:
<Application x:Class="WpfApplication1.App"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml"
ShutdownMode="OnMainWindowClose">

The process should then automatically terminate when the main window is closed even if some or all of the child windows are still hidden.

Feedback