2

I am using a window to change database configuration for my application. Settings button click is handeled by Config(), it show settings windows when clicked. If I close the using cross sign on the right corner of window, I am not able to reuse the windows it shows following exception.

Cannot set Visibility or call Show, ShowDialog, or WindowInteropHelper.EnsureHandle after a Window has closed.

ConfigDialogBox configDlg = new ConfigDialogBox();

        private void SettingsChanged(object sender, RoutedEventArgs e)
        {
            Database.host = configDlg.host;
            Database.port = configDlg.port;
            Database.user = configDlg.user;
            Database.password = configDlg.password;
            Database.database = configDlg.database;            
            ConfigDlg.Visibility = Visibility.Hidden;
        }

        private void Config(object sender, RoutedEventArgs e)
        {            
            configDlg.Show();
            configDlg.okButton.Click+=new RoutedEventHandler(SettingsChanged);
            configDlg.cancelButton.Click+=new RoutedEventHandler(SettingsChanged);
        }

        void cancel_Click(object sender, RoutedEventArgs e)
        {         
           ConfigDlg.Visibility = Visibility.Hidden;
        }

How can I reuse it after closing?

user913359
  • 660
  • 7
  • 14
  • Why do you need to re-use it, couldn't you just create a new one? – tehlexx Jun 27 '12 at 06:01
  • This windows contains database configuration and I will lose all settings if the windows is closed. It is working fine with ok and cancel, only close causing problem. – user913359 Jun 27 '12 at 06:06

3 Answers3

2

You can't reuse the window.

If closing the window via something other than OK and Cancel buttons is your problem, you need to handle the Window.Closing event (see the link for an example).

Botz3000
  • 39,020
  • 8
  • 103
  • 127
0

You can't. Just create a new window.

durron597
  • 31,968
  • 17
  • 99
  • 158
Dennis
  • 37,026
  • 10
  • 82
  • 150
  • Actually all settings will be lost if window is closed that is not desired. – user913359 Jun 27 '12 at 06:05
  • I think the change you are suggesting is adding configDlg = new ConfigDialogBox(); before configDlg.show, it is not solving my problem. – user913359 Jun 27 '12 at 06:07
  • Don't store settings in the `ConfigDialogBox` instance. Save it elsewhere, when closing dialog with 'OK' button. – Dennis Jun 27 '12 at 06:09
0
ConfigDialogBox configDlg = null;



private void Config(object sender, RoutedEventArgs e)
        {            
         configDlg = new ConfigDialogBox();
            configDlg.Show();
            configDlg.okButton.Click+=new RoutedEventHandler(SettingsChanged);
            configDlg.cancelButton.Click+=new RoutedEventHandler(SettingsChanged);
        }
JohnnBlade
  • 4,261
  • 1
  • 21
  • 22
  • 2
    or u can cancel the onclosing event with e.Cancel = true; – JohnnBlade Jun 27 '12 at 06:24
  • also look here, cause u would need a persistent way of storing yr settings http://stackoverflow.com/questions/453161/best-practice-to-save-application-settings-in-a-windows-application – JohnnBlade Jun 27 '12 at 06:29