简体   繁体   中英

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.

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

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().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM