How to skip Validating after clicking on a Form's Cancel button

54,440

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:

  1. Insure that the Cancel button (if any) has .CausesValidation set to false
  2. 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.

Share:
54,440

Related videos on Youtube

user228985
Author by

user228985

Updated on November 02, 2020

Comments

  • user228985
    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
      Stewbob over 14 years
      Nothing you do to the Cancel button will prevent the Validating code from executing when your textbox loses focus.
  • Stewbob
    Stewbob over 14 years
    Not sure why someone down-voted this. For a winforms application, it is true. The other three answers are incorrect.
  • Tim Schmelter
    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 from MouseEnter of the BtnCancel(didn't work either).
  • Mark Ransom
    Mark Ransom over 10 years
    Microsoft should set the default CausesValidation value to False for containers, since they don't take focus by themselves anyway.
  • dotNET
    dotNET over 9 years
    If 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
    dotNET about 9 years
    Here 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
    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 to false 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
    Kevin S. Miller about 7 years
    It doesn't do it for me.
  • Jamesckel
    Jamesckel over 5 years
    This single setting worked just fine for me, setting CausesValidation on the cancel button to "false".