Clay Shannon

Clay Shannon

  • NA
  • 8
  • 5k

Why does DateTimePicker refuse to update its value?

May 4 2012 5:38 PM

It seems DateTimePicker is buggier than woodpecker heaven.

I've got a form-global bool, IgnoreDTPValueChangedEvent, that is set to true when declared, but then false in the DateTimePicker's MouseDown event. Also a form-global DateTime var named OldDate.

I try to use the ValueChanged() event to preempt a user from changing dates (actually, it's too late to prevent them, but I try to change the date BACK to the previous one (OldDate) if appropriate.

However, even though the code SEEMS to be working in the debugger, in actuality (as far as what the eye can see when looking at the DateTimePicker at runtime), it does not: the debugger says the DateTimePicker's value and text HAVE been changed back, as intended, but that is not reflected in the control itself, its appearance, that is.

I try to "force" it to straighten up and fly right, using the call to Update() (I've also tried Invalidate()), but nothing works.

private void dateTimePickerScheduleDate_ValueChanged(object sender, EventArgs e) {

 if (IgnoreDTPValueChangedEvent) return;

 if (CanChangeDate()) {
  OldDate = dateTimePickerScheduleDate.Value;
  PopulateInterpreterScheduleVals();
  SynchronizeIntArrays();
  return;
 }

 IgnoreDTPValueChangedEvent = true;
 dateTimePickerScheduleDate.Value = OldDate; // stepping though, it looks good
 dateTimePickerScheduleDate.Text = dateTimePickerScheduleDate.Value.ToString(); // " "
 dateTimePickerScheduleDate.Update(); // also tried Invalidate()
}

DateTimePicker may be a pinball wizard, but when it comes to sensory deprivation, it seems to be a poster child for that.