You can validate the data as the data is entered in a control by reading the keys as they are pressed, or more commonly whenever the user takes the input focus away from one control and moves to the next.This approach is useful when you want to give the user immediate feedback about the data as they are working.

c validating textbox input-29c validating textbox input-32

If you want full programmatic control over validation, or need to perform complex validation checks, you should use the validation events built into most Windows Forms controls.

Each control that accepts free-form user input has a Validating event that will occur whenever the control requires data validation.

You can still use control validation to lock focus to a control, but you do not have to be concerned about the behavior associated with closing the form.

I have some code that checks and makes sure that when the users enters in the field an integer from 1 - 10 has to be input.

If you want to use implicit validation for a control, you must set that control's .

If you assigned Enable Prevent Focus Change, canceling the event will cause the Validated event not to occur.For example, if you have code in a Validated event that attempts to cancel the data binding, the data binding will still occur.In this case, to perform validation in the Validated event, change the control's Data Source Update Mode property (under (Databindings)\(Advanced)) from On Validation to Never, and add The implicit validation approach validates data as the user enters it.The Format event on a data binding can be used to reformat incoming data to comply with the mask, and the Parse event can be used to reformat outgoing data to comply with the specifications of the data field.For more information, see Masked Text Box Control (Windows Forms).In the Validating event-handling method, you can validate user input in several ways.