So often, I want the user to select a start date and a finish date. But just selecting date is not enough, we also have to alter the data.
By default, the DateTimePicker.Value are like
Value 1: 2012-01-01 10:12:09
Value 2: 2012-01-02 10:12:09
When the user selects two dates, it should be obvious that he meant
Value 1: 2012-01-01 00:00:00
Value 2: 2012-01-02 23:59:59
I often forget to do the non-intuitive
DateTime start = dateTimePicker1.Value.Date;
DateTime finish = dateTimePicker2.Value.Date.AddDays(1).AddSeconds(-1);
What more effective way of dealing with this have you found?
If you use
DateTimePickerobjects lke this a lot, you could possible create two small custom classes: aStartDateTimePickerand anEndDateTimePicker. Each class would derive fromDateTimePicker, and would simply have a boolean and an EventHandler on the OnValueChanged event. The event would be used to adjust the value after it is set, and the boolean would be used to implement the Balking Pattern. Here’s an example of theStartDateTimePicker:You would then simply use these in place of your normal
DateTimePickerobjects and you would not have to worry about making sure that the dates are appropriately adjusted anymore.It would cost you the time to write the
EndDateTimePickerclass (the above is already a fully functionalStartDateTimePicker), but it would make things easier down the road as you use these in more places.