I have created two buttons (button1, button2), and they both have a click routedevent handler.
When button1 is clicked, it opens an instance of the second window (Window2), which run along with the main window (MainWindow).
I would like when I click button2 to close both windows (window2, MainWindow) and show a third window (Window3).
What I did so far
private async void Button1_Click(object sender, RoutedEventArgs e)
{
var win_window2= new Window2();
win_window2.Cursor = Cursors.Wait;
win_window2.Show();
//Here the Window2 is shown along with the MainWindow
}
When I click button2 I would like to close both the MainWindow and Window2
private void Button2_Click(object sender, RoutedEventArgs e)
{
Window3 win_window3 = new Window3();
win_window3.Show();
this.Close(); //close MainWindow
//What to type here to also close win_window2?
}
What I tried so far based on this SO answer is
private void CloseAllWindows()
{
for (int intCounter = App.Current.Windows.Count - 1; intCounter >= 0; intCounter--)
App.Current.Windows[intCounter].Close();
}
private void Button2_Click(object sender, RoutedEventArgs e)
{
CloseAllWindows(); //This will close also Window3
Window3 win_window3 = new Window3();
win_window3.Show();
//What to type here to also close win_window2?
}
But the methods CloseAllWindows()
and Environment.Exit(0)
will also close the Window3, which I want to avoid. I want only to close MainWindow and Window2().
Any suggestions on that? Thanks a lot.