Click here to Skip to main content
13,512,345 members
Rate this:
Please Sign up or sign in to vote.
See more:
I added a button and wrote;

private void Button_Click_1(object sender, RoutedEventArgs e)
            MainWindow_1 mw = new MainWindow_1();
            MainWindow mw1 = new MainWindow();
//mw.Close(); doesnt work as well//

by this i open a new form but mw doesn't close.

How can i do that?
Posted 5-Feb-13 11:54am
Why do you think that you are closing "itself"? Why do you think you are closing anything at all?
Hslldm 5-Feb-13 18:29pm
I have a "enter" window and it has a button "enter". when i click it,
antoher window(mw1)
is opened.

And that time i want the "enter" window" closed.
I answered, please see Solution 1.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You are not trying to hide anything. The Window mv is not yet shown, so hiding it has no effect. Also, hiding is not closing. If, by some reason, you see some window (except mw1) which is still showing, this is some other window, which has nothing to do with invisible window mv, maybe you real main window.

By the way, if a window class is called MainWindow, it does not mean it is really main. The main window is the one used as a parameter of Application.Run(Window),[^].

If create your WPF application is a "usual" way, through the Visual Studio template, this call to Application.Run is auto-generated, so you may not see it, but it always exists in your application.

And please don't ask what should you do: you shouldn't do anything like what is written in your code; it just makes no sense. In particular, you should not close a main window, it would close the application.

One of the approaches it to call Application.Run several times, each time with a different window, but I don't think you really need it. You should better think on your application design.

Rate this: bad
Please Sign up or sign in to vote.

Solution 2


If any window is opened or any application is running that resembles to the process. You can stop the process or kill the process object that is responsible for opening the window. In this way you can able to close the window that you want to close.

Hslldm 6-Feb-13 7:03am
How can i do that?
arnavs 6-Feb-13 7:16am
You can check the process is open or not like the following:
public bool IsProcessOpen(string name)

foreach (Process clsProcess in Process.GetProcesses())

if (clsProcess.ProcessName.Contains(name))

return true;
//otherwise we return a false
return false;
If the process is open then close the process.

like the following:

string filePath = @"C:\\Users\\sisirp\\Desktop\\Phrase Translation Util\\PhraseTranslationUtil.exe";

System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(filePath, "");

//System.Diagnostics.Process p = System.Diagnostics.Process.Start(info);
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo = info;

//while (p.Responding)
while (IsProcessOpen("PhraseTranslationUtil"))
Hslldm 6-Feb-13 7:21am
Thank you but;
Also worked. But there is another problem. When i open the WPF by button. It
is opened behind the WPF which is exist. So i can't see that. How can i take the
new WPF to the front?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.180417.1 | Last Updated 5 Feb 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100