3
votes

I'm still at the learning stage of c# so don't shoot me if you see this as a dumb question ;-)

In the project I have the main window, and an new window called "window1"

On the main, I make a button, that will go to the window1 like this:

private void Button_Click(object sender, RoutedEventArgs e)
{

    Window1 W1 = new Window1();
    W1.Show();
    this.Close();

}

Now, this works as expected, creating a new instance of window1 and show it while closing the mainwindow.

But here is the catch: in window1 some stuff can change, like a button that the user clicks, and then that button gets hidden while a new one shows.

Also I made a back button on window1, that uses the same code as above to go back to the mainWindow (so now new window1 but new mainwindow)

But at this point, when I click in main, on the button again to go to window1, that window1 is back to default state. Seems logical to me because it creates a new instance when using the button.

But how should I do this if I want to open that first instance of window1 again, the one that already had been changed by the user?

First I thought of placing the Window1 W1 = new Window1(); outside of the button method, but this also won't work because of that "back" button.

I hope I explained well enough what I'm trying to do.

Any thoughts an this?

=====================

EDIT 1:

using the code example from "chrfin" some parts genarate errors, like the "visible = true" part maybe this is because i use express 2010 or it is because i use WPF and not forms?

in the main:

Window1 W1 = null; // Initialise Field.  

private void CalcTabel_Click(object sender, RoutedEventArgs e)
{

    if (W1 == null)
    {
        W1 = new Window1();
        W1.MainWindow = this; //ERROR 
        W1.Show();
    }
    else
        W1.Visibility = Visibility.Visible;
    this.Visibility = Visibility.Hidden;
}

in the window1

public MainWindow w1 { get; set; }

private void Quit_Click(object sender, RoutedEventArgs e)
{
    w1.Visibility = Visibility.Visible;
    this.Visibility = Visibility.Hidden;

}

now the error that i get on the main part is: 'WpfApplication1.Window1' does not contain a definition for 'MainWindow' accepting a first argument of type 'WpfApplication1.Window1' could be found(are you missing a using directive or an assembly reference?)

just removing that error line, will cause the get,set part not to get anything.

any ideas ?

=====================

EDIT 1:

Thanx again "chrfin" got it working now :)

in main:

Window1 W1 = null; // Initialise Field.  

private void CalcTabel_Click(object sender, RoutedEventArgs e)
{

    if (W1 == null)
    {
        W1 = new Window1();
        W1.Hoofdmenu = this;
        W1.Show();
    }
    else
        W1.Visibility = Visibility.Visible;
    this.Visibility = Visibility.Hidden;
}

in Window1:

public MainWindow Hoofdmenu { get; set; }

private void Quit_Click(object sender, RoutedEventArgs e)
{

    Hoofdmenu.Visibility = Visibility.Visible;
    this.Visibility = Visibility.Hidden;

}
  • Solved -
5
maybe juste set visible false? - Christophe Debove
i think you could Hide the Window form and bring it back later on - V4Vendetta
+1: On SO there's no such thing as a dumb question, just badly asked ones. Everyone has to learn, as long as they take the effort to make their question clear and well put (which you have) - Jon Egerton
You named your property in Window1 w1, but you are accessing it with .MainWindow so changed them to be the same... - Christoph Fink

5 Answers

2
votes

You could do something like this:

Window1 w1 = null;

private void Button_Click(object sender, RoutedEventArgs e)
{
    if(w1 == null)
    {
        w1 = new Window1();
        w1.MainWindow = this; //create this property - see below
        w1.Show();
    }
    else
        w1.Visible = true;

    this.Visible = false;
}

and inside Window1:

public MainWindow MainWindow { get; set; }    

private void ButtonBack_Click(object sender, RoutedEventArgs e)
{
    this.Visible = false;
    MainWindow.Visible = true;
}
1
votes

There are many ways of achieving the desired outcome. The method I would use is below :

Window1 W1 = null; // Initialise Field. 

private void Button_Click(object sender, RoutedEventArgs e)
{
    if (W1 == null) W1 = new Window1();
    W1.Show();
    this.Close();
}
0
votes

You could also cancel the event.

0
votes

Well there can be more than one solution here.

1) You can preserve the changes made by user in Windows1. Whenever you re-launch the Window1 one pass it the setting or previous changes so that Window1 Can re-adjust itself according to the last state

2) Instead of always creating a new instance of Window1 and closing it on back button just use show and hide to make window1 visible and invisible to user

0
votes

Make a static property to store the reference of W1 in it and in Button_Click method check if it has some value use it otherwise create a new one;

Another option is to look at open windows in application object accessible through Application.Current.Windows.