Realuther
Realuther

Reputation: 63

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.

Upvotes: 0

Views: 1602

Answers (2)

Assasin Bot
Assasin Bot

Reputation: 136

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

Upvotes: 1

Dblaze47
Dblaze47

Reputation: 868

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

Upvotes: 1

Related Questions