Close window and return to previous window in application

Answered Close window and return to previous window in application

  • Thursday, May 31, 2012 7:09 PM
     
      Has Code

    I have a WPF app that im working on, the first Window you get is a login. If you dont have an account, there is a "signup" button which takes you to another window where you can sign up. I added a "cancel" button but cant figure out how to close that current registration window and return to the login window..

    I have tried .Hide(), .Close() and even .Visibilty set to hidden or colapsed and none work except the .Close(), but it closes the entire application.

    This is the code i have in my "MainWindow.cs"

            Login frm;
            private void DisplayLoginScreen()
            {
                frm = new Login();
                frm.btnSignUp.Click += new RoutedEventHandler(btnSignUp_Click);
                bool? result = frm.ShowDialog();
                if (result.HasValue && result.Value)
                {
                  
                }            
                else
                {
                    //this.Close();
                }
            }
            void btnSignUp_Click(object sender, RoutedEventArgs e)
            {
                Registration rwind = new Registration();
                rwind.ShowDialog();
            }

    Just trying to figure out what i need to put in the close event on the registration window.

            private void btnCancel_Click(object sender, RoutedEventArgs e)
            {
                //What do i put here
            }

All Replies

  • Thursday, May 31, 2012 8:16 PM
     
      Has Code

    hi,

    you can use the Action in singup Window.

    write this code in close button Window:

                public Action CloseAction;

    and you can create event in btnSignUp_Click:

    Registration rwind = new Registration();
                rwind.CloseAction = new Action(() =>
                    {
                        rwind.Close();
                    });
    rwind.ShowDialog();

    and write code in btnCancel_Click:

    CloseAction();


  • Thursday, May 31, 2012 8:33 PM
     
     

    Thanks, but its closing my entire application, not the registration window.

    :(

  • Thursday, May 31, 2012 8:53 PM
     
     

    Check the ShutdownMode of your application.  Most likely it is set to "On last window close".  This means that as soon as there are no more windows open the app will close.  Generally you would handle this by opening the MainWindow and if you have any other dialogs (login or other) you open them (modal or modeless) and deal with them in that manner.

    Hope this helps

    Lloyd Sheen


    Lloyd Sheen

  • Wednesday, June 06, 2012 11:03 AM
    Moderator
     
     

    Hi Cubangt,

    How is the problem now? Do you resolve it?

    Cannot repro the scenario, I tested the .Close() as well as the Ali's solution, both work.

    Please let us know if you have new findings.

    Thanks,


    Kee Poppy [MSFT]
    MSDN Community Support | Feedback to us

  • Wednesday, June 06, 2012 1:52 PM
     
      Has Code

    Lloyd's suggestion didnt help either:

    <Application x:Class="MyApp.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 ShutdownMode="OnExplicitShutdown">

    What would i need to to post or provide that i havent yet to see if anyone can help?

    time permitting i can zip up the files and what i have for someone to review..

  • Thursday, June 07, 2012 3:24 AM
    Moderator
     
     

    You could update your project to Skydrive and paste the access link here, so we can download and help testing it.

    Have a nice day,


    Kee Poppy [MSFT]
    MSDN Community Support | Feedback to us

  • Friday, June 08, 2012 4:56 PM
     
     

    I have created a new test project with just the pages that are causing me my issues. I have zip it posted on my site, which you can download from here

    wpfapplication2

    Thank you

  • Monday, June 11, 2012 10:45 AM
    Moderator
     
     Answered Has Code

    @ Cubangt:

         void btnSignUp_Click(object sender, RoutedEventArgs e)
            {
                Registration rwind = new Registration();
                rwind.CloseAction = new Action(() =>
                {
                    rwind.Close();
                    DisplayLoginScreen();
                });
                rwind.ShowDialog();
            }
    Use this code instead of your original one, it then should get to work.

    Kee Poppy [MSFT]
    MSDN Community Support | Feedback to us

    • Marked As Answer by Cubangt Tuesday, June 12, 2012 8:47 PM
    •  
  • Tuesday, June 12, 2012 8:47 PM
     
     

    ok, that seems to be working..

    WOW!!! thank you so much.. what a difference 1 line of code can make..