Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a new window, but not always a new instance of it

Tags:

c#

window

show

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 -
like image 333
Dante1986 Avatar asked Dec 14 '25 16:12

Dante1986


1 Answers

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;
}
like image 83
Christoph Fink Avatar answered Dec 17 '25 07:12

Christoph Fink



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!