Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
4.33/5 (3 votes)
See more:
Hi,
I am stuck in my WPF application. I have a user control, on which I have placed many different link(buttons) which would open other forms. Now on the click of those links I want a single form to open and let all be closed. The code I am using is as below, and I know that this would never let this happen the way I want :

C#
private void btnOEP_Click(object sender, RoutedEventArgs e)
        {
           ExistingProject objEP = new ExistingProject();
             CloseAllWindows();
             objEP.ShowDialog(); 
        }

        private void btnCNP_Click(object sender, RoutedEventArgs e)
        {
            AddNewProjects objAddNew = new AddNewProjects();
            CloseAllWindows();
            objAddNew.ShowDialog();
           
        }

private void CloseAllWindows()
       {
           for (int intCounter = App.Current.Windows.Count - 1; intCounter >= 0; intCounter--)
               App.Current.Windows[intCounter].Close();
       }



The problem which I am facing is that I am opening a form dialogue, and after that I am closing all the form windows of the application(using this code), which in turn will will close the required window from also. I need to close all the forms except the one I have given the command to stay open. How could this be done. Please suggest a solution.

Thanks
Posted
Updated 7-Nov-22 18:29pm
v5
Comments
Henry Minute 15-Oct-10 8:53am    
As I said before, you cannot close the main form because doing so will shut the application down. There is always System.Window.Hide() which will hide the main window but you will then have to pass a reference to the main window to each new window that you create, handle the OnClosing() event of that new window to Show() the main window again to give access to the buttons, ready for next time.

I have just set up a test project using your code.

With a couple of mods it works for me.

Firstly, you are using ShowDialog(), this means that you can no longer access the main form to launch another window without first closing the previous one manually.

So bearing that in mind, this works for me:

C#
private void btnOEP_Click(object sender, RoutedEventArgs e)
{
  CloseAllWindows();
  ExistingProject objEP = new ExistingProject();
  objEP.Show();
}

private void btnCNP_Click(object sender, RoutedEventArgs e)
{
  CloseAllWindows();
  AddNewProjects objAddNew = new AddNewProjects();
  objAddNew.Show();
}

private void CloseAllWindows()
{
  for (int intCounter = App.Current.Windows.Count - 1; intCounter > 0; intCounter--)
      App.Current.Windows[intCounter].Close();
}
 
Share this answer
 
Comments
@nuraGGupta@ 15-Oct-10 8:44am    
Thank you so much henry, although, it keeps open the main form, its worth the pain, (I will be glad if I could find out the method to close the main form also)......I am your fan now, thanks for the help and time.
Have a nice time.
I can not cross check this because at present I am at home and I don't have VS installed on my machine.
C#
NewWindow objNew = new NewWindow(); //Create an object of the window you want to open
CloseAllWindows(); //close all other windows
objNew.ShowDialog(); //Now use the ShowDialog command, and I guess it will work fine as the object reference has been created in the memory.


Provide your feedback if it doesn't goes right.


Anurag
 
Share this answer
 
C#
public void CloseAllWindows()
       {
           for (int intCounter = App.Current.Windows.Count - 1; intCounter >= 0; intCounter--)
               App.Current.Windows[intCounter].Hide();
       }
 
Share this answer
 
Comments
alvinchesaro 10-Oct-18 11:43am    
Thanks! This one worked for me
OK.

Here's what I think is the problem.

The App.Current.Windows collection contains all the windows for your application, including its main window which should be App.Current.Windows[0].

Therefore if you close every window in the collection you are closing the main window. When the main window closes it closes the application.

So I think that if you modify your code to either:

C#
private void CloseAllWindows()
{
    for (int intCounter = App.Current.Windows.Count - 1; intCounter > 0; intCounter--)
        App.Current.Windows[intCounter].Close();
}


or:
C#
private void CloseAllWindows()
{
    for (int intCounter = App.Current.Windows.Count - 1; intCounter >= 1; intCounter--)
        App.Current.Windows[intCounter].Close();
}


then it should work.

Hope this helps. :)
 
Share this answer
 
Comments
@nuraGGupta@ 15-Oct-10 8:05am    
sorry to say, but its still showing the same exception :
System.InvalidOperationException:
"Cannot set Visibility or call Show or ShowDialog after window has closed."
try to it..
Window win = Application.Current.Windows.OfType().SingleOrDefault(w => w.Name == "Window Name");
win.Close();
 
Share this answer
 
C#
Login login = App.Current.Windows.OfType<Login>().FirstOrDefault();
           if(login != null)
           {
               login.Close();
           }
 
Share this answer
 
You can do this :

C#
Login login = App.Current.Windows.OfType<Login>().FirstOrDefault();
           if(login != null)
           {
               login.Close();
           }
 
Share this answer
 
You can use App.Current.WindowName.Close()
Example:

C#
private void Button_Click(object sender, RoutedEventArgs e)
        {
            NewWindow newWindow = new NewWindow();
            newWindow.Show();
            App.Current.OldWindow.Close();
        }

Hope it's helpful!
 
Share this answer
 
Comments
Graeme_Grant 8-Nov-22 0:57am    
why answer a 12-year-old question with accepted answers?
Richard Deeming 8-Nov-22 4:06am    
Especially when the new solution doesn't actually do what the OP said they wanted. 🤦‍♂️

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