Overriding DialogResult of a button on a form

  • Thread starter Thread starter BobRoyAce
  • Start date Start date
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?
 
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
 
Back
Top