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.
Dialog boxes commonly allow users to accept or cancel the task for which they were shown before the dialog box is closed. ShowDialog returns a Nullable<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.
You must use the using statement here. ShowDialog() returns a DialogResult value, don't ignore it or you'll try to use the dialog properties when the user canceled the dialog.
ShowDialog() Shows the form as a modal dialog box. ShowDialog(IWin32Window) Shows the form as a modal dialog box with the specified owner.
Show() method shows a windows form in a non-modal state. ShowDialog() method shows a window in a modal state and stops execution of the calling context until a result is returned from the windows form open by the method.
http://msdn.microsoft.com/en-us/library/system.windows.window.showdialog.aspx
ShowDialog returns a
Nullable<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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With