Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

.net

dialog

wpf

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.

like image 362
Alex Baranosky Avatar asked Feb 03 '09 00:02

Alex Baranosky


People also ask

How do I return something on ShowDialog?

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.

How to return a value from form. ShowDialog in c#?

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.

What is ShowDialog()?

ShowDialog() Shows the form as a modal dialog box. ShowDialog(IWin32Window) Shows the form as a modal dialog box with the specified owner.

What is the difference between show and ShowDialog in C#?

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.


3 Answers

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.

like image 177
JMD Avatar answered Oct 23 '22 03:10

JMD


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.

like image 35
Matt Hamilton Avatar answered Oct 23 '22 04:10

Matt Hamilton


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.

like image 42
Nir Avatar answered Oct 23 '22 02:10

Nir