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).
Given that you’ve specified you want a pop error dialog, one way of doing this is to move your validation into a
Form.OnFormClosingevent handler. In this example the form close is aborted if the user answers yes to the question in the dialog.By setting
e.Cancel = trueyou will prevent the form from closing.However, it would be a better design/user experience to display the validation errors inline (via highlighting the offending fields in some way, displaying tooltips, etc.) and prevent the user from selecting the OK button in the first place.