I have a Windows Form with TextBox controls and a Button on it. The Validating event of each TextBox makes sure that only well formed input can be submitted (duh). The Button is a clear button that resets the form, clearing the text boxes values, etc. If I have written something illegal in a TextBox and then press the clear button, the validation of course fires and I get a MessageBox telling me this. So I can’t use the Clear button unless I manually clear the invalid TextBox.
I realize that the reason the Validating event fires is that the TextBox loses focus when I press the clear button, but is there a way to ignore validation in this case?
I tried setting the Form.AutoValidate to disabled but then I never get any validation, which isn’t exactly what I want.
It should be sufficient for you to set the
CausesValidationproperty of the clear button tofalse. This way validation is not triggered if you have the text box selected and press the button.From MSDN entry for CausesValidation: