C# – Validating or Validated event

cwinforms

I have some textboxes databound to a BindingSource to custom business object. Databinding is done as the following code-

  txtBookNo.DataBindings.Add("Text", bs, "BookNo", true, DataSourceUpdateMode.OnPropertyChanged, null, "G", GlobalVariables.CurrentCultureInfo);

Now I would like to validate user input. Which event is best for this purpose- Textbox.Validating or Textbox.Validated? And, what should be DatasourceUpdateMode- OnPropertyChanged or OnValidation?

Thanks.

Best Answer

  • Validating is fired just before the validation process starts, this is the place to put the actual validation code.
  • Validated is fired after the validation process has finished and is designed to be the place to something based on the validation result.

If DataSourceUpdateMode.OnPropertyChanged is used then:

Data source is updated whenever the value of the control property changes.

If DatasourceUpdateMode.OnValidation is used then:

Data source is updated when the control property is validated, After validation, the value in the control property will also be re-formatted.

If you want only valid (that means it has been validated by your code) data to be sent to your custom business objects you shuld use DataSourceUpdateMode.OnValidation.

Here is an article explaining how to do validation in Windows Forms.

Related Topic