Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8092011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:11:48+00:00 2026-06-05T20:11:48+00:00

It appears that second windows are not closing. Here is what I am doing….

  • 0

It appears that second windows are not closing. Here is what I am doing….

  • I have created a new WPF project.

  • I placed a button in the main window.

  • I put the following code in the button to launch a second window and hide the main.

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        var projectWindow = new NewProjectInfo();
        this.Hide();
        projectWindow.Show();
    }
    

-The new window is a blank window with nothing on it.

-In the “close” event of the new window I put the code to show the main window again.

    private void Window_Closed(object sender, EventArgs e)
    {
        var mainWindow = new MainWindow();
        mainWindow.Show();
    }
  • If I run the program and close the main window without opening the second window, the programs ends like I would like it to.

  • If I click the button on the main window, taking me to the second window, then close that window, taking me back to the main window, then close the main window the program does not end. (meaning I have to hit the stop button in visual studio to get it to end)

It’s like visiting the second window leaves some kind of process running.

So, when I close the main window I can just run the:

 Application.Current.Close();

But is that safe? Do I run the chance of leaving something hanging? Or is there a better way to close that second window so when I close the main the program will end?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T20:11:49+00:00Added an answer on June 5, 2026 at 8:11 pm

    These lines

       var mainWindow = new MainWindow(); 
       mainWindow.Show(); 
    

    create a new MainWindow and show it. The original MainWindow is still hidden.

    To resolve your problem, you need a reference to the original MainWindow instance.
    Perhaps you could pass this reference when you build your secondary window

    // Create a NewProjectInfo instance and pass this 
    var projectWindow = new NewProjectInfo(this); 
    this.Hide(); 
    projectWindow.Show(); 
    

    And in the constructor of the projectWindow, save the reference passed in a global var

    var MainWindow _originalReference;
    
    public NewProjectInfo(MainWindow original)
    {
        _originalReference = original;
    }
    

    now the window close event could use the referenced window

    private void Window_Closed(object sender, EventArgs e)   
    {   
        _originalReference.Show();   
    }   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It appears that we will have to build/deploy one of our new JBoss apps
I have a Windows form that's generated using code (including buttons and what not).
I have and old AV that hunts me. It appears randomly (often but not
Appears that WAS does not support the integration binding. I've tried setting it up
I have a C# project in VS 2008, that is being rebuilt every time
I need to have a dropdown that appears after I select the first dropdown,
I have inherited a Java project which includes a number of classes that are
I have a windows service that is, intermittently, throwing an exception that sometimes kills
In a Windows Form, I have a search box that fires an event to
Okay, I have a TreeView that serves as a directory tree for Windows. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.