When is it good to use the DialogResult for a Button

T

Tony Johansson

Hello!

This is just an example I have two forms called Form1 and Form2.

In Form1 I have the following event handler for a button called btnShowForm2
private void btnShowForm2_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
DialogResult dr = frm2.ShowDialog();
if (dr == DialogResult.OK)
MessageBox.Show("User clicked OK button");
else if (dr == DialogResult.Cancel)
MessageBox.Show("User clicked Cancel button");
}

In Form2 I have two buttons called OK and Cancel.
The property DialogResult is set to OK for button OK and
DialogResult is set to Cancel for button Cancel.

So when I start the application and Form1 is is shown I can click on button
btnShowForm2 which
will show Form2 with the two buttons OK and cancel.
If I click on the OK button Form2 will be closed and the message box will
display User clicked OK button and if I click the cancel button Form2 will
be closed and the messagebox will display User clicked Cancel button.

Now to my question can somebody give me a scenario when the above
functionality will be useful ?
I mean when is the point for Form1 to know that OK button is returning
DialogResult = OK in this example and
the Cancel button return DialogResult=Cancel.
How can in any way Form1 be using this information from Form2's OK button
and Cancel button

//Tony
 
J

Jeff Johnson

Tony Johansson said:
Hello!

This is just an example I have two forms called Form1 and Form2.

In Form1 I have the following event handler for a button called
btnShowForm2
private void btnShowForm2_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
DialogResult dr = frm2.ShowDialog();
if (dr == DialogResult.OK)
MessageBox.Show("User clicked OK button");
else if (dr == DialogResult.Cancel)
MessageBox.Show("User clicked Cancel button");
}

In Form2 I have two buttons called OK and Cancel.
The property DialogResult is set to OK for button OK and
DialogResult is set to Cancel for button Cancel.

So when I start the application and Form1 is is shown I can click on
button btnShowForm2 which
will show Form2 with the two buttons OK and cancel.
If I click on the OK button Form2 will be closed and the message box will
display User clicked OK button and if I click the cancel button Form2 will
be closed and the messagebox will display User clicked Cancel button.

Now to my question can somebody give me a scenario when the above
functionality will be useful ?
I mean when is the point for Form1 to know that OK button is returning
DialogResult = OK in this example and
the Cancel button return DialogResult=Cancel.
How can in any way Form1 be using this information from Form2's OK button
and Cancel button

The DialogResult property is just a convenience property when it comes to
buttons. In the Click event handlers for the OK and Cancel buttons you can
explicitly set the form's DialogResult property, for example:

private void cancelButton_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
// You can stick "this." in front of the property if you
// like to be really clear. I'm not a fan of the clutter....
}

If you set cancelButton.DialogResult = DialogResult.Cancel in design mode or
code then you don't even need a Click event handler; clicking Cancel will
immediately close the dialog and set the form's DialogResult property to
Cancel.

(I hope that was your question, i.e., "What's the point of the property on
buttons?" and not "Why do I care if a dialog returns OK or Cancel?")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top