Using Dialogresult Correctly

Using DialogResult Correctly

When you open a modal dialog with ShowDialog, the calling code is blocked until the form called closes or hides. If you want to read some public properties of the called form and want to do things (for example save data to a database or to a file) based on the click on the OK or Cancel button, then you need to know if the user wants to do the action or not. The DialogResult returned by the ShowDialog() method allows you to take the appropriate actions...

So for example

using (Form1 form = new Form1())
{
DialogResult dr = form.ShowDialog();
if(dr == DialogResult.OK)
{
string custName = form.CustomerName;
SaveToFile(custName);
}

}

An important thing to add to this answer is the fact that the DialogResult property exists both on the Form class and in the Button class. Setting the button's DialogResult property (both via code or designer) to a value different from DialogResult.None is the key to activate an important behavior for forms. If you click a button with that property set then the Forms Engine transfers the value of the Buttons property to the Forms one and triggers the automatic closure of the form reactivating the caller code. If you have an event handler on the button click then you can run code to validate the form's inputs and force the form to stay open overriding the form's DialogResult property setting it back to DialogResult.None

For example, in the modally showed form you can have:

// Event handler for the OK button set with DialogResult.OK
public void cmdOK_Click(object sender, EventArgs e)
{
// Your code that checks the form data and
// eventually display an error message.
bool isFormDataValid = ValidateFormData();

// If data is not valid force the form to stay open
if(!isFormDataValid)
this.DialogResult = DialogResult.None;
}

Using System::Windows::Forms::DialogResult::OK to go back to previous form

You have:

testing_f->ShowDialog();
if (testing_f->ShowDialog() == System::Windows::Forms::DialogResult::OK)

Having the standalone ShowDialog call doesn't really make sense and does explain why you have to hit the button twice (it's actually different instances of the dialog, it just appears so quickly you can't see that).

Custom C# button's DialogResult doesn't work correctly on modal form

You need to actually apply the assigned DialogResult property, it is not automatic. Do so by overriding the OnClick() method:

protected override void OnClick(EventArgs e) {
var form = this.FindForm();
if (form != null) form.DialogResult = dialogResult;
base.OnClick(e);
}

Technically you should also notify accessibility clients of the state change, it isn't clear at all whether you care about this. It tends to be skipped for custom controls but you generally ought to. Insert these statements before the base.OnClick() call:

    base.AccessibilityNotifyClients(AccessibleEvents.StateChange, -1);
base.AccessibilityNotifyClients(AccessibleEvents.NameChange, -1);

DialogResult.OK not working properly C#

If you use default Show("Some text") method or specify only one button for other override of Show methods you'll always get the same "OK" result. So no matter what way you close the "OK" message box (button, "ESC" or "X") you get OK as result and your if condition will be satisfied.

To enable other options you should specify on of multi-button MessageButtons enum values:

DialogResult dialogResult = 
MessageBox.Show("You Select " + NumberOfStorey + " Storey!",
"Selection", MessageBoxButtons.OKCancel);

Will give you DialogResult.OK and DialogResult.Cancel results.

Custom form always return DialogResult.Cancel

Calling Close will set the DialogResult property to Cancel overriding whatever you set before the call to Close. You can easily verify this using the debugger and inspecting the value of this.DialogResult before and after the call to Close.

But, when a form is shown modally you don't need and you shouldn't normally call Close. You can hide the form just setting the property DialogResult causing your code to exit from the call to ShowDialog.

A form shown modally is not closed when you set the DialogResult property but only hidden. This allows the calling code to access the form's properties and take appropriate actions.

Also, it is good practice to enclose the form's initialization call in a using statement to propertly Dispose the modal form when you don't need it anymore. (Of course this is not the case with forms shown not-modally)

using(RegisterForm registerForm = new RegisterForm())
{
DialogResult dialogResult = registerForm.ShowDialog();
if(dialogResult == DialogResult.OK)
{
.....
}
}
// <== At this point the registerForm instance has been Closed and Disposed
// It is no more in scope and you cannot use it in any way
....
private void btRegister_Click(object sender, EventArgs e)
{
DialogResult = !string.IsNullOrEmpty(Key) ?
DialogResult.OK : DialogResult.None;
}

private void btCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
}

Windows Forms DialogResult

public void xyz() {
var dialogResult = booleanVariable ? new Form1().ShowDialog() : new Form2().ShowDialog();

if (dialogResult == DialogResult.OK) {
...
}
}

Trouble with Form.DialogResult

Perhaps your dialog isn't setting the dialog result. Make sure your OK and Cancel buttons have their DialogResult properties set to what you expect.

Why is Dialogresult nil?

I found the answer to my question.

When you want to use a winform purely as a dialog box, then you CANNOT have FormClosing event.

For my thewinform, I accidently created its FormClosing event and forgot about it.

method thewinform.thewinform_FormClosing(sender: System.Object; e: System.Windows.Forms.FormClosingEventArgs);
begin
e.Cancel := true;
hide;
end;

Once I removed this winform event, ShowDialog and DialogResult is behaving as expected.

This is very similar to another stackoverflow question Why does ShowDialog always return DialogResult.Cancel?



Related Topics



Leave a reply



Submit