Close only one Window

Asked

Viewed 1,271 times

1

I created a window JanelaUm, every time I click on button this window is displayed.

in the code-behind of JanelaUm have the event window_KeyDown with the following code:

if (e.Key == Key.Escape)
    this.Close();

But if the user clicks 5 times on button to display the JanelaUm, when closing using the button esc will close all five windows, not just one.

Would anyone have an idea of how to close only one? and the other 4 remain open?

  • 1

    How you create and do display of windows?

  • JanelaUm JUm = new JanelaUm(); JUm.ShowDialog();

  • In that form, you don’t have a button that makes a cancel?

  • No, just the button itself form.

  • i couldn’t reproduce the problem, if you can, post more code from your example, use git to upload the project if possible.

  • 1

    I don’t see how you can open 5 windows if you’re using ShowDialog()? When opening the first phocus stays in it and you can no longer access the initial window until you close the one that was opened.

  • Yeah, I made a mistake Show();

  • The @Lucasmotta response, solved the problem, is closing only the active window, the others remain open.

  • 1

    I find that strange because only the active window should receive the key. Just out of curiosity, where(Event) is that you are testing the key?

  • and at the event private void window_KeyDown(object sender, KeyEventArgs e)

Show 5 more comments

1 answer

2


You can do it this way

   if (e.Key == Key.Escape && this.IsActive)
        this.Close();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.