Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF VS2012
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 5-Feb-13 12:54pm
Hslldm393
Comments
Sergey Alexandrovich Kryukov at 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 at 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 at 5-Feb-13 19:14pm
   
I answered, please see Solution 1.
—SA
Rate this: bad
good
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), 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
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Comments
Hslldm at 6-Feb-13 7:03am
   
How can i do that?
Hslldm at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 531
1 OriginalGriff 315
2 Maciej Los 295
3 Peter Leow 260
4 CPallini 180
0 Sergey Alexandrovich Kryukov 7,138
1 OriginalGriff 5,577
2 Peter Leow 4,097
3 Mika Wendelius 2,850
4 CHill60 2,818


Advertise | Privacy | Mobile
Web03 | 2.8.150224.1 | Last Updated 5 Feb 2013
Copyright © CodeProject, 1999-2015
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