Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
1.00/5 (1 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();
            mw1.Show();
            mw.Hide();
//mw.Close(); doesnt work as well//
}

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

How can i do that?
Posted
Comments
Sergey Alexandrovich Kryukov 5-Feb-13 18:12pm    
Why do you think that you are closing "itself"? Why do you think you are closing anything at all?
—SA
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.
Sergey Alexandrovich Kryukov 5-Feb-13 19:14pm    
I answered, please see Solution 1.
—SA

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), http://msdn.microsoft.com/en-us/library/ms597011.aspx[^].

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.

—SA
 
Share this answer
 
v3
Hi,

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.

Thanks
 
Share this answer
 
Comments
Hslldm 6-Feb-13 7:03am    
How can i do that?
[no name] 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"))
{
//p.Start();
p.Close();
}
Hslldm 6-Feb-13 7:21am    
Thank you but;
-------------
WPF1.Show();
this.Close();
-------------
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900