Cancel click event on ShowDialog form

I am displaying a form with xForm.ShowDialog(this);

.

This form has a button that is DialogResult

set to OK. Now when I click OK, some validation is done. If that fails, I want the form to stay that way, but the form closes and returns DialogResult.OK

to the main form. So how can I prevent this?

For instance,

Button_click
{
    If validation fails pretend we never came here
    else some code.. return DialogResult.OK
}

      

0


a source to share


3 answers


Don't assign DialogResult to a button. A method that returns DialogResult can be used, which calls the Form.ShowDialog method. If the Validate button is clicked, validate the form and, if validation completed, return the expected DialogResult.



-1


a source


Button_click()
{
  if (this.Validate())
  {
    this.DialogResult = DialogResult.OK ;
  }

}

      



+3


a source


In your FormClosing event of the dialog, set the FormClosingEventArgs Cancel property to true if you don't want the form to close.

0


a source







All Articles