0

I need some assistance in having ShowDialog appear more than once as in this case 2x without getting the error as follows: Cannot set Visibility or call Show, ShowDialog, or WindowInteropHelper.EnsureHandle after a Window has closed. Any help is appreciated.

namespace Application
{
    public partial class App : Application
    {
        int count = 0;
        int logonAgreements = 2;

        while (count < logonAgreements)
        {     
            DialogResult = lf2.ShowDialog(logonAgreements, count + 1);

            if (DialogResult == true)
            {        
                count++;
            }
            else if (DialogResult == false)
            {
                Close();
            }
        }   

        public DialogResult ShowDialog(int numCustomer, int currentCustomerIndex)
        {   
            this.labelPanelHeader.Content = "Log-On Agreement" + " (" + currentCustomerIndex + " of " + numCustomer + ")";
            return this.ShowDialog();
        }

    }

}
Pedro
  • 1
  • 1
  • 3
  • 1
    The error message seems pretty clear to me. `ShowDialog()` doesn't return until the window is actually closed, and you're not allowed to show a closed window again. – Peter Duniho Oct 23 '15 at 00:22
  • It'd be helpful if you'd explain why it is you feel you should keep the original dialog window object, instead of just creating a new one as needed each time you want to show it. As the error says, you can't really do this anyway -- once the dialog is closed, it can't be shown again. And normally, the dialog state would be represented by a separate view model object, which you can reuse as often as you want. If you provide specific details as to what you're actually trying to do, you might get a good answer that addresses your broader problem, even though the immediate problem can't be fixed. – Peter Duniho Oct 23 '15 at 03:39
  • See also [How to detect that a Window has used up its “ShowDialog” call](http://stackoverflow.com/q/4179758) – Peter Duniho Oct 23 '15 at 03:58
  • I created the window again through the use of the while loop. Window lf2 = new Window(); and that seemed to work just how I needed it to. – Pedro Oct 23 '15 at 17:45

1 Answers1

-1

You simply can't reuse it after closing it since some of its resources have been disposed.

Instead, on Closing event, cancel the close, and set the visibility to hidden. Once you want it to be shown again, set it to visible.

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        e.Cancel = true;
        this.Visibility = Visibility.Hidden;
    } 
Ahmad Hammoud
  • 701
  • 1
  • 5
  • 15
  • He must then create EventHandlers, or that's at least what I can think of. – Ahmad Hammoud Oct 23 '15 at 00:32
  • Actually, I misspoke. Hiding a dialog causes the `ShowDialog()` method to return (the documentation is incorrect that it only returns when the window is _closed_). However, there is a different problem: having only hidden the window and not closed it, the process won't exit normally even when the main window is closed. There are also potential rendering issues next time the window is shown. A good answer would provide a complete solution, not one that creates more new questions. – Peter Duniho Oct 23 '15 at 00:45
  • The thing is he's insisting to reuse his form again, and since I've read about it earlier, I loved to give him a way to do it, though it's not that good to use.. – Ahmad Hammoud Oct 23 '15 at 00:49
  • _"though it's not that good to use"_ -- no, the problem is it is not even possible to get things to work correctly using your code. Yes, you can avoid the specific exception, but all kinds of other things just don't work (e.g. the window isn't displayed correctly the next time it's shown, and the process won't exit). If you have a solution for all those other problems, you should include all those details in your answer; if you don't, you shouldn't suggest the above as an answer in the first place. – Peter Duniho Oct 23 '15 at 00:52
  • Thanks for clarifying. – Ahmad Hammoud Oct 23 '15 at 00:54