Question

How do I get System.Windows.ShowDialog() to return 'true'?

I am a little new to this. System.Windows.ShowDialog's return type is bool? It is supposed to return true when you hit Submit, and false when you hit Cancel. But I am not sure how to designate which Button is the official submit button.

EDIT: On a related note, I am curious as to how it can return null.

Was it helpful?

Solution

http://msdn.microsoft.com/en-us/library/system.windows.window.showdialog.aspx

ShowDialog returns a Nullable<(Of <(T>)>) Boolean value that specifies whether the activity was accepted or canceled. The return value is the value of the DialogResult property before a window closes (see DialogResult).

Basically, you decide by setting the value of the DialogResult, not by hitting a particular button -- you decide what the button does.

OTHER TIPS

In WPF, set the Button.IsDefault property to true to specify that a button is the "submit" button for a window. I'm not 100% sure that this will make the window close with a DialogResult of true. If it doesn't, you just need to handle its Click event thusly:

this.DialogResult = true;

Edit

Likewise, you can use the Button.IsCancel property to have a button be the "cancel" button for a form.

Edit 2

I believe the reason ShowDialog is nullable is that since it's null up until the form is submitted or canceled, you could test for that if you were watching the dialog in a background thread. I haven't tried that, but it seems like a logical reason why they'd introduce a third "unknown" (null) state to the property.

if you set DialogResult to true ShowDialog returns true, if you set DialogResult to false ShowDialog returns false if the dialog is closed without setting DialogResult (the user clicks on the red X in the top right corner) ShowDialog will return null.

Setting IsDefault to true will cause the button to look a little different and pressing enter will "click" this button.

If you set IsCancel to true the pressing esc will "click" this button.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top