tags:

views:

592

answers:

5

I have a window that I sometimes open using Show() and sometimes using ShowDialog(). In the second case, the returned dialog result is important for me. But if I set the DialogResult after calling Show() I get an InvalidOperationException. Is there a way to find out which method was used to open the window and set or not the DialogResult accordingly? Or is there another way?

Of course I know I can catch and ignore the exception, but I don't like this solution.

+1  A: 

How about just setting this.DialogResult = DialogResult.blah in the form closing event?

sgrassie
How would that help? That would still set it even after Show() and cause the exception.BTW I use WPF (I didn't specify that clearly) so the DialogResult is bool?.
svick
before I do this.Close(); in the Modal Window I set this.DialogResult = DialogResult.OK; and in the main Form this is exactly what I get. Thank you sgrassie :)
balexandre
+2  A: 

Use System.Windows.Interop.ComponentDispatcher.IsThreadModal inside the window to determine if it runs on a modal thread or not.

Pop Catalin
Thanks, that's exactly what I wanted.
svick
A: 

If you look at set_DialogResult in Reflector, it checks _showingAsDialog to determine whether the dialog is modal. Unfortunately this is a private field.

Do you always construct a new instance of the window before calling Show()/ShowDialog(). If so, you could pass an argument to the constructor indicating how it is to be shown.

Phil Devaney
A: 
 if (this.Modal)
            {
                MessageBox.Show("Dialog");
            }
            else
            {
                MessageBox.Show("Form");
            }
adatapost
That works only in WinForms, I use WPF.
svick
A: 

You can use the Form.Modal property to check the kind of usage.

In the case of using Form.Show() you have to use another way to let the caller know of any results of the Form.

Is there a reason to use both ways of showing the form?

Frank Bollack
That property doesn't exist in WPF
McAden