I have a dialog that I show with <class>.ShowDialog()
. It has an OK button and a Cancel button; the OK button also has an event handler.
I want to do some input validation in the event handler and, if it fails, notify the user with a message box and prevent the dialog from closing. I don't know how to do the last part (preventing the close).
Best Answer
You can cancel closing by setting the Form's
DialogResult
toDialogResult.None
.An example where button1 is the AcceptButton:
When the user clicks button1 and the validate method returns false, the form will not be closed.