Overriding DialogResult of a button on a form

B

BobRoyAce

I have a form that I show as a dialog (i.e. Form.ShowDialog). On this
form I have two buttons: one with a DialogResult of OK, and the other
with a DialogResult of Cancel. However, if the user clicks on the one
with a DialogResult value of OK, and something's wrong with data they
enetered on the form, I want to show a message and cancel the closing
of the form? Is there a way, in the Click event for the button, to say
"Hey, by the way, don't close the form!" I am thinking that I could
just set the DialogResult of the button to None and then set it to OK
if all is well. Is that the best way to do this?
 
L

Lars

BobRoyAce said:
I have a form that I show as a dialog (i.e. Form.ShowDialog). On this
form I have two buttons: one with a DialogResult of OK, and the other
with a DialogResult of Cancel. However, if the user clicks on the one
with a DialogResult value of OK, and something's wrong with data they
enetered on the form, I want to show a message and cancel the closing
of the form? Is there a way, in the Click event for the button, to say
"Hey, by the way, don't close the form!" I am thinking that I could
just set the DialogResult of the button to None and then set it to OK
if all is well. Is that the best way to do this?
You would normally handle this in the FormClosing event:
if (this.DialogResult == DialogResult.OK)
{
if (someConditionIsNotMet)
{
MessageBox.Show("Please fix whatever");
e.Cancel = true;
return;
}
}

Lars
 

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