0

I want to be able to press a button and have the program open up a new window and close the old one.

I have followed solutions from this link but i have never has success with any of them How do I open a second window from the first window in WPF?

Here is my work soo far:

Window editor = new Window();
editor.Show();
this.Close();

But this does nothing.

The program should open up a new window and close the old one.

vasily.sib
  • 3,871
  • 2
  • 23
  • 26
Realuther
  • 63
  • 1
  • 10

2 Answers2

1

The functionality you described will work just fine. The Problem there is would more likely be the function or Methode in which you call this function.

To write a Methode that would handle a Button press as you want is pretty good described here: https://www.c-sharpcorner.com/forums/c-sharp-button-click-hold-and-release.

Hopefully, this will help you otherwise just ask

here is a small Implementation if that helps:

public partial class MainWindow : Window
{
    private void MainWindow_KeyDown(object sender, KeyEventArgs e)
    {
        Window editor = new MainWindow();
        editor.Show();
        this.Close();
    }

    private void MainWindow_KeyUP(object sender, KeyEventArgs e)
    {

    }
    public MainWindow()
    {
        this.KeyDown += MainWindow_KeyDown;
        this.KeyUp += MainWindow_KeyUP;
    }
}
Bilaal Rashid
  • 828
  • 2
  • 13
  • 21
Assasin Bot
  • 136
  • 1
  • 1
  • 11
1

You have to call the second window from the first. This is something I did for a project where it popped up a new login panel window:

 private void displayLoginPanel_Click(object sender, EventArgs e) 
 {
     LoginPanel myLogin = new LoginPanel(this);
     myLogin.Show();
     this.Hide();
 }

I used hide() instead of close() because you can see that I am sending a reference of the parent to the child LoginPanel in order to come back later. You can replace the Hide() with Close().

Dblaze47
  • 868
  • 5
  • 17