I have a c# winforms app with a lot of different controls that are bound (via bindingsource) to different properties of my business object (and this object implements INotifyPropertyChanged). When my user sets a certain control to a particular value, I need to change the value of another property (in code) and perhaps even disable the control that binds to that property. For example, I have a datetimepicker that maps to a datetime property. If my user enters a time value less than 00:10:00 (HH:MM:SS), I need to uncheck a checkbox (which is bound to a bool property). Currently, I have code in a validating event handler that looks at the value in the datetimepicker and if the .Hour == 0 and the .Minute < 10, I programatically set the value of the bool property (that the checkbox is bound to) equal to false. When I do this, the checkbox becomes unchecked, but the value in the datetimepicker reverts to what it was before the user entered the time that caused the boolean property to be set to false in the first place.
If I watch the value of the datetimepicker in the debugger, it gets reset as soon as this line of code executes:
businessObject.booleanVariable = false;
Do I need to do something to force the datetimepicker value back to the object property before I programatically set the boolean property to false?!? Or am I going about this all wrong? Is there some other way that bound controls which may impact other bound controls should be handled?
The
Validatingevent fires before data is sent back to the bound object. Its purpose is to allow you to ensure that a control contains data that is valid according to business logic, and using it for handling ordinary (non-validation) logic will cause problems like you’re seeing.The best course of action would be to add code in the setter of your property that sets the
boolproperty.