1
votes

I'm work on WPF project, let say I have two windows window1,window2, window1 call window2: in window1 code behind :

Window2 _window2 = new Window2();
_window2.ShowDialog();

What I want to know how I close first window (window1) after the secound window (window2) is opened ?

3
Is WIndow1 your startup window?Myrtle
No, both windows not startup windowYoMo

3 Answers

1
votes

If "Window1" is not your main window you can just type this

Window2 _window2 = new Window2();
_window2.Show();
this.Close();

If your form is the main form than refer (Windows Forms: Change application mainwindow at runtime)

0
votes

Before you can close the main window (if it is), then you need to tell the app first which is the main window. Depending upon your shut down mode, if you close the main window, you shut down the app. You can set this using Application.Current.MainWindow property. See Here.

If you wish to close the first window after opening the second, you will need to hook up an event, or better still use a decoupled messaging system to inform the first window the second has opened.

0
votes

Form1:

Window2 _window2 = new Window2();
this.Hide(); //Hides Form 1
_window2.ShowDialog();

Also see: Close a Window from Another In Wpf if you wanna have window1 closed.