Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#WPFVS2012
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 11:54am
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
Your Filters
Interested
Ignored
     
0 Shai Vashdi 339
1 Sergey Alexandrovich Kryukov 305
2 OriginalGriff 290
3 Peter Leow 171
4 Guruprasad.K.Basavaraju 170
0 Sergey Alexandrovich Kryukov 8,979
1 OriginalGriff 5,280
2 Peter Leow 4,010
3 Maciej Los 3,535
4 Abhinav S 3,263


Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 5 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid