Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 982
user228985 Avatar asked Dec 10 '09 17:12

user228985


People also ask

How do you prevent a button from validating its form?

To prevent validation from being performed, set the CausesValidation property to false . You should set the CausesValidation property to false when you are using the PostBackUrl property to post back to a different page. You should explicitly check validation when posting back to a different page.

How do you set a cancel button?

On any Windows Form, you can designate a Button control to be the cancel button. A cancel button is clicked whenever the user presses the ESC key, regardless of which other control on the form has the focus.

How do you validate a form?

Basic Validation − First of all, the form must be checked to make sure all the mandatory fields are filled in. It would require just a loop through each field in the form and check for data. Data Format Validation − Secondly, the data that is entered must be checked for correct form and value.


12 Answers

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

like image 69
Daniel Schaffer Avatar answered Oct 05 '22 18:10

Daniel Schaffer


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.

like image 35
Vimal Raj Avatar answered Oct 05 '22 18:10

Vimal Raj


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();
}
like image 40
Ben Avatar answered Oct 05 '22 19:10

Ben


Set the CausesValidation property of the Cancel button to false.

like image 40
womp Avatar answered Oct 05 '22 17:10

womp


Set the CausesValidation property to false.

like image 44
Brandon Avatar answered Oct 05 '22 17:10

Brandon


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.

like image 39
Wade Hatler Avatar answered Oct 05 '22 18:10

Wade Hatler


Setting CausesValidation to false is the key, however this alone is not enough. If the buttons parent has CausesValidation set to true, the validating event will still get called. In one of my cases I had a cancel button on a panel on a form, so I had to set CausesValidation = false on the panel as well as the form. In the end I did this programatically as it was simpler than going through all the forms...

Control control = cancelButton;

while(control != null)
{
   control.CausesValidation = false;
   control = control.Parent;
}
like image 28
Steve Sheldon Avatar answered Oct 05 '22 18:10

Steve Sheldon


In my case, in the form I set the property AutoValidate to EnableAllowFocusChange

like image 39
Alecs Avatar answered Oct 05 '22 19:10

Alecs


By using Visual Studio wizard you can do it like that:

enter image description here

like image 41
Oscar Castiblanco Avatar answered Oct 05 '22 18:10

Oscar Castiblanco


Judicious use of the Control.CausesValidation property will help you achieve what you want.

like image 28
Joe Avatar answered Oct 05 '22 19:10

Joe


Just above the validation code on the edit box add:

if (btnCancel.focused)
  {
     return;
  }

That should do it.

like image 30
TonyM Avatar answered Oct 05 '22 19:10

TonyM


In complement of the answer of Daniel Schaffer: if the validation occurs when the edit box loses focus, you can forbid the button to activate to bypass local validation and exit anyway.

public class UnselectableButton : Button
{
    public UnselectableButton()
    {
        this.SetStyle(ControlStyles.Selectable, false);
    }
}

or if you use DevExpress:

this.simpleButtonCancel.AllowFocus = false;

Note that doing so will change the keyboard experience: the tab will focus anymore on the cancel button.

like image 44
Olivier de Rivoyre Avatar answered Oct 05 '22 19:10

Olivier de Rivoyre