I have a simple winforms app with one form, a few controls and a business object defined like this:
public class BusinessObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
{
PropertyChanged(this, e);
}
}
private string _phoneNumber;
public string PhoneNumber
{
get { return _phoneNumber; }
set
{
if (_phoneNumber == value)
{
return;
}
_phoneNumber = value;
OnPropertyChanged(new PropertyChangedEventArgs("PhoneNumber"));
}
}
On my form, I have a textbox that is bound to the PhoneNumber property via a binding source and the data source update mode is set to OnPropertyChanged. This all works as expected. I need to do some validation on the text before it gets assigned to the PhoneNumber property on my business object. I thought that I would do this in the Validating event handler for the textbox and, if the input is invalid, I display my error provider and set e.Cancel = true. Unfortunately, this doesn't prevent the invalid input from being bound to the PhoneNumber property. Is there an easy way to do this?
As suggested by mrlucmorin, I've changed my update data source mode from "OnPropertyChanged" to "OnValidation" so that the binding only occurs when the textbox loses and gets validated. I did implement validation in the Validating() event handler for my textbox and set e.Cancel = true when the data is invalid. Unfortunately, clicking buttons on my toolbar doesn't seem to cause the textbox to lose focus so the Validating() event never fires but I was able to work around that by calling ValidateChildren() when a toolbar button is clicked. Thanks again to mrlucmorin and ImGreg for the suggestions that ultimately solved my problem!
Data Validation might be just the thing you are looking for. Should keep invalid input from changing your objects.
According to the msdn, the event you are using is occurring after the value has changed. One option is to store a backup of the data and restore the value that changed. However, this is not an ideal approach.
I would change the how you are validating the controls.
I'm not certain when to do this as it depends on how your code works. Maybe perform your own validation when you lose focus on the textbox control or do the validation when the datasource is to be updated.
EDIT: Perhaps you are looking for the ErrorProvider Class. This can be used to handle validation like you want.