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
None of these answers quite did the job, but the last answer from this thread does. Basically, you need to:
- Insure that the Cancel button (if any) has .CausesValidation set to false
-
Override this virtual method.
protected override bool ProcessDialogKey(Keys keyData) { if (keyData == Keys.Escape) { this.AutoValidate = AutoValidate.Disable; CancelButton.PerformClick(); this.AutoValidate = AutoValidate.Inherit; return true; } return base.ProcessDialogKey(keyData); }
I didn't really answer this, just pointing to the two guys who actually did.
Related videos on Youtube
user228985
Updated on November 02, 2020Comments
-
user228985 over 3 years
I use C#. I have a Windows Form with an edit box and a Cancel button. The edit box has code in validating event. The code is executed every time the edit box loses focus. When I click on the Cancel button I just want to close the form. I don't want any validation for the edit box to be executed. How can this be accomplished?
Here is an important detail: if the validation fails, then
e.Cancel = true;
prevents from leaving the control.
But when a user clicks Cancel button, then the form should be closed no matter what. how can this be implemented?
-
Stewbob over 14 yearsNothing you do to the Cancel button will prevent the Validating code from executing when your textbox loses focus.
-
-
Stewbob over 14 yearsNot sure why someone down-voted this. For a winforms application, it is true. The other three answers are incorrect.
-
Tim Schmelter over 11 years@DanielSchaffer: i'm not sure if I understand the "however"-part correctly. How can i prevent the validation from taking place when the user hits the cancel button? The validation triggers a messagebox which is always shown even if the user wants to cancel editing/adding an object because the validating happens before the button-click. I've even tried to set
CausesValidation=false
fromMouseEnter
of theBtnCancel
(didn't work either). -
Mark Ransom over 10 yearsMicrosoft should set the default
CausesValidation
value toFalse
for containers, since they don't take focus by themselves anyway. -
dotNET over 9 yearsIf I have nested containers, do I have to set it to
False
all the way up for all containers, or just the immediate parent of my button? -
dotNET about 9 yearsHere again after a few months :). And I figured it out myself later on. You do not need to set it all the way up for all parent containers. Just setting the immediate parent is sufficient.
-
Imperishable Night about 7 years@Stewbob I think the first sentence is incorrect. The Validating event of a textbox by definition occurs when the textbox loses focus, and in case it is losing focus to the cancel button, setting
CausesValidation
tofalse
will stop exactly this from happening. The answer would lead one to believe that the validation happens and then the cancel button works, while in reality no validation happens at all. -
Kevin S. Miller about 7 yearsIt doesn't do it for me.
-
Jamesckel over 5 yearsThis single setting worked just fine for me, setting CausesValidation on the cancel button to "false".