Close Window after open another window? - c#

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 ?

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)

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.

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.

Related

Close another window in WPF

I'm building a multi-window app.
The problem I've got, I can't close "Window1" or "Window2" from button in "MainWindow".
All windows are in the same app.
Is there a way to kill another window?
Sure there is. If you have a reference to that Window, just call the Close() method.
Window window1 = new Window1();
window1.Show();
...
window1.Close();

How can I proceed only if the window I just opened is closed?

I have two WPF windows.
Window1's button calling Window2, Window2 updates something, and then I want to proceed exactly where I stopped in Window1.
This is the code in Window1 inside a button_Click:
Window2 window = new Window2();
window.Show();
* wait until Window2 closes
//continue with other actions
How do I wait?
I saw this: wpf c# button wait for a button press
But how can I pass it from a different (already closed) window?
Use Window.ShowDialog method, it opens a window and and waits for window closing.
http://msdn.microsoft.com/en-us/library/system.windows.window.showdialog.aspx
Nullable<bool> dialogResult = window.ShowDialog();
// Test dialogResult: it is true if dialog was closed by OK button
Windows used as modal dialogs are usually contain OK and Cancel buttons, and programmed by such way, that pressing OK returns true from ShowDialog methid.
See also: Creating a Modal Custom Dialog Box
http://msdn.microsoft.com/en-us/library/aa969773.aspx
Use Window.ShowDialog() instead of Show(). This will open a modal window and will wait till it closes

Wpf Main Window Get Minimized

How can i keep my main window maximized in wpf app.
when i open second window and do something in that and close it, my main window get minimized.
please help me.
in fact i have three window:
A main window that application start with that.
A second window that shows a list of entities. with edit, delete and add button.
The third window that is for editing the selected entity.
when i close the third and second window, my main window get minimized.
i use the below code for opening second and third windows:
SecondWindow win = new SecondWindow();
win.Owner = this;
win.ShowDialog();
thanx.
I think Your problem is: something about how you show Main-dialog not other Forms!. Can you provide how Show Main-dialog?
also you can try use WindowState="Maximized" on your Main-dialog.

Make Window1 disappear and launch another Window

I have 2 Windows in my sample App (learning .Net 4 WPF)
In the first window, I have a timer and when 5 seconds passed, i want to close the current Window and open a new Window.
The problem I'm running into is closing the first window
Here's some sample code
MainWindow m = new MainWindow();
m.ShowDialog();
this.Hide();
this.Hide never actually hides the current window. I end up with 2 windows on my screen instead of 1.
In the remarks of ShowDialog it says When this method is called, the code following it is not executed until after the dialog box is closed.
So, you can just swap the order of ShowDialog and Hide. and you have to use 'Show' or 'Close' after 'ShowDialog' to either display back first form or close it.
Also, note that closing a form (what you said want to do) is different to hiding a form (what you're currently doing).
MainWindow m = new MainWindow();
this.Hide();
m.ShowDialog();

C# WPF child window (about window)

I have an application on C#/WPF that I`m developing ... I have a btn named About which should open a new window that contains details about the application or whatever I will put in it.
When I click the btn, a new window (about) opens, when I click again, while the new window (about) is opened another one is opened, how can I prevent this from happening, I also want the application to be disabled when the About window is opened and to be enabled when the About window is closed, Like most of the applications when about is clicked.
You should use the ShowDialog Method: http://msdn.microsoft.com/en-us/library/system.windows.window.showdialog.aspx
Code Sample:
// Instantiate window
AboutWindow aboutWindow = new AboutWindow();
// Show window modally
// NOTE: Returns only when window is closed
Nullable<bool> dialogResult = aboutWindow.ShowDialog();
Just use ShowDialog() method instead of Show()
AboutWindow aboutWindow = new AboutWindow();
aboutWindow.ShowDialog();

Categories