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

Viewed 54868

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?

17 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

Set the CausesValidation property of the Cancel button to false.

Set the CausesValidation property to false.

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

enter image description here

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

This is an old question however I recently ran into this issue and solved it this way:

1st, we are loading a UserControl into a 'shell' Form that has a save and cancel button. The UserControl inherit an interface (like IEditView) that has functions for Save, Cancel, Validate and ToggleValidate.

In the shell form we used the mouse enter and mouse leave like so:

    private void utbCancel_MouseEnter(object sender, EventArgs e)
    {
        ((Interface.IEdit)tlpMain.Controls[1]).ToggleValidate();
    }

    private void utbCancel_MouseLeave(object sender, EventArgs e)
    {
        ((Interface.IEdit)tlpMain.Controls[1]).ToggleValidate();
    }

Then in ToggleValidate (Say a simple form with two controls...you can always just loop through a list if you want) we set the CausesValidation

    public bool ToggleValidate()
    {
        uneCalcValue.CausesValidation = !uneCalcValue.CausesValidation;
        txtDescription.CausesValidation = !txtDescription.CausesValidation;

        return txtDescription.CausesValidation;
    }

Hope this helps.

I found this thread today while investigating why my form would not close when a validation error occurred.

I tried the CausesValidation = false on the close button and on the form itself (X to close).

Nothing was working with this complex form.

While reading through the comments I spotted one that appears to work perfectly

on the form close event , not the close button (so it will fire when X is clicked also)

This did the trick.

AutoValidate = AutoValidate.Disable;

Create a bool:

bool doOnce;

Set it to false in your function and then:

if (doOnce == false)
{
    e.cancel = true;
    doOnce = true;
}

This means it will only run once and you should be able to cancel it. This worked for me anyways.

Related