This is a two part question I guess.
I have a form that allows a user to edit an existing record stored in a SQL database. One of the items the users can edit must have a value when the record is edited using this particular form, but it does accept null values on the server side so validation won't work there. So for this particular form a value must be validated because the SQL server will not throw an exception if it is blank since, in some instances it can be. The value is being evaluated by a custom validation event that I have written (it's nothing fancy, just 3 lines) but I have a problem...
The form contains 3 buttons:
- Revert - Reverts the record back to the state it was when it was opened in the editor.
- Cancel - Cancels changes and closes the editor.
- Save - Saves the data to the server and closes the form.
The main problem I have is that my validation code uses
e.Cancel = true; if validation fails which, if I understand correctly, indicates that validation failed and the events following should be canceled, including closing the form, reverting data back to it's original state or even changing to a different control.
That's great and fine, but because of that, the revert and cancel buttons don't work unless the user fixes the validation problem first, which kind of defeats the purpose of a cancel button or revert button.
Here's the question:
On the form, I have defined the AcceptButton and CancelButton properties to the corresponding "Save" and "Cancel" buttons on my form. When validating my controls, is there a way to find out if the cancel button was clicked and to "skip" validation without iterating through all of the controls and turning validation off?
More of a "if the form is closing because the user clicked this button, then just return as validated so the form can close" type of logic. Or maybe just validate the textbox under certain circumstances, like when a particular button is clicked.
I've started to dabble in ASPX deveopment to supplement my form application, and I've noticed with validation controls you can assign validation "Groups" so that if a form contains multiple buttons, if one control would fail validation when a button is clicked, you can include another button that only validates controls in a particular list...does that exist in WinForms?
A supplemental question as well...when defining a CancelButton property on a form, no code is required to actually close the form in the button's click event handler, is it permissible to just leave it that way or should I still have an event handler that is either blank, or one that still calls