Tuesday, July 8, 2008

Bound TextBox Control Validation Issue

Level: Beginner

Knowledge Required:
Data Binding

Description:
Sometimes it happens that we bound TextBox with BindingSource for a Numeric Column, if we try to put a non-numeric value or a very large numeric value in that TextBox, the application stuck, i.e. cannot get out from TextBox, cannot close the Form. This is because internal validation is running.

Whenever we try to get out from TextBox control or try to close the Form, validation happens and if value (provided in TextBox) is NOT a legal value for that particular column then validation fails, which causes the focus to remain in the TextBox control.

To solve this issue just set the CausesValidation property of TextBox control to False. This will stop the automatic Validation.

1 comment:

Anonymous said...

Validation is necessary in order to apply changes to the underlying datasource. Instead of turning off validation, you can handle it via the textbox.validating event.

You can either fix the data (i.e. replace empty string with zero for numeric field, etc.)

Or display a validation error, so the user knows why the cursor is not leaving the field.