Search code examples
c#wpfwindow

How Do I Call Another Window With Button Press?


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.


Solution

  • 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;
        }
    }