How to skip Validating after clicking on a Form's Cancel button
Solution 1:
If the validation occurs when the edit box loses focus, nothing about the the cancel button is going to stop that from happening.
However, if the failing validation is preventing the cancel button from doing its thing, set the CausesValidation
property of the button to false
.
Reference: Button.CausesValidation
property
Solution 2:
Obviously CausesValidation
property of the button has to be set to false and then the validating event will never happen on its click. But this can fail if the parent control of the button has its CausesValidation
Property set to true. Most of the time developers misses/forgets to change the CausesValidation
property of the container control (like the panel control). Set that also to False. And that should do the trick.
Solution 3:
I was having problems getting my form to close, since the validation of certain controls was stopping it. I had set the control.CausesValidation = false
for the cancel button and all the parents of the cancel button. But still was having problems.
It seemed that if the user was in the middle of editing a field that was using validation and just decided to give up (leaving the field with an invalid input), the cancel button event was being fired but the window would not close down.
This was fixed by the following in the cancel button click event:
private void btnCancel_Click(object sender, EventArgs e)
{
// Stop the validation of any controls so the form can close.
AutoValidate = AutoValidate.Disable;
Close();
}
Solution 4:
Set the CausesValidation
property of the Cancel button to false
.
Solution 5:
Set the CausesValidation
property to false
.