locked
Reopening a dialog box after it is closed. RRS feed

  • Question

  • I have the following code:

    ----------------------------------------

    Window1 test =  new Window1();

    private void button1_click(object sender, RoutedEventArgs e)

    {  

         test.Owner = this;

         test.ShowDialog();

    }

    ---------------------------------

    If using Windows forms: On the Widows1 form I have a button with the property set as CancelButton. When I cancel it I can re-open it with the button click event on the main form without a problem.

    When I try the same thing in WPF with the button on Windows1 property set to IsCancel, I get an Exception error: Cannot set visibility or call show [...] after a window has closed.

    Any solutions to this?



    • Edited by LB27468 Monday, September 23, 2013 8:55 AM
    Monday, September 23, 2013 8:49 AM

Answers

  • Not really. The Form.Close method actually closes the form. However, a form is not disposed on Close when the following two conditions are met (http://msdn.microsoft.com/en-us/library/system.windows.forms.form.close.aspx):

    (1) It is part of a multiple-document interface (MDI) application, and the form is not visible; and (2) you have displayed the form using ShowDialog.

    In these cases, you will need to call Dispose manually to mark all of the form's controls for garbage collection.

    Monday, September 23, 2013 12:45 PM

All replies

  • You can't reopen a closed window. Instead, you should create a new instance of the window every time you open it:

        private void button1_click(object sender, RoutedEventArgs e) {
          Window1 test =  new Window1();
          test.Owner = this;
          test.ShowDialog();
    
        }
    

    ..or alternatively change the visibility of the window rather than closing it:

      public partial class Window1 : Window
      {
        public Window1() {
          InitializeComponent();
          this.Closing += Window1_Closing;
        }
    
        void Window1_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
          this.Visibility = System.Windows.Visibility.Hidden;
          e.Cancel = true;
        }
    
        private void Button_Click(object sender, RoutedEventArgs e) {
          this.Close();
        }
      }
    

    • Proposed as answer by Joba Diniz Friday, September 27, 2013 2:06 AM
    Monday, September 23, 2013 9:07 AM
  • Hi Thanks for your reply.

    I am copying code from another application which was created using Windows Forms Application. The code works as I have written it in a Windows Forms Application. It does not work in WPF.

    So when you say "You can't reopen a closed window", does that only apply to WPF?

    Monday, September 23, 2013 9:53 AM
  • In Windows Forms you open and close instances of System.Windows.Forms.Form objects whereas in WPF you have System.Windows.Window objects, so there is certainly a difference.
    Monday, September 23, 2013 10:01 AM
  • Thanks again for your reply.

    Would I be right in saying that the Cancel property in the code is performing the same function as the form hide method?

    If I use this.Hide() in WPF the above code works.

    So basically, is the cancel really just hiding the Form in the Windows Form application?

    Is using this.Hide() in WPF achieving the same thing?

    Thanks.

    Monday, September 23, 2013 10:53 AM
  • Not really. The Form.Close method actually closes the form. However, a form is not disposed on Close when the following two conditions are met (http://msdn.microsoft.com/en-us/library/system.windows.forms.form.close.aspx):

    (1) It is part of a multiple-document interface (MDI) application, and the form is not visible; and (2) you have displayed the form using ShowDialog.

    In these cases, you will need to call Dispose manually to mark all of the form's controls for garbage collection.

    Monday, September 23, 2013 12:45 PM