pinkfloydhomer
pinkfloydhomer

Reputation: 905

Ignoring validation of TextBoxes when pressing a specific button

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.

Upvotes: 2

Views: 517

Answers (1)

João Angelo
João Angelo

Reputation: 57658

It should be sufficient for you to set the CausesValidation property of the clear button to false. This way validation is not triggered if you have the text box selected and press the button.

From MSDN entry for CausesValidation:

Gets or sets a value indicating whether the control causes validation to be performed on any controls that require validation when it receives focus.

Upvotes: 1

Related Questions