My case is that, the application is accepting raw data file with datetime precision up to microsecond, and WinForms is using DateTimePicker
and allow the user to amend the datetime.
However, according to this, the CustomFormat
property allows precision only up to second only.
The behavior is like that: When I set
this.dateTimePicker.Value = DateTime.Now;
I can get the value back directly with the microsecond part preserved.
But when the user re-selects a date by the DateTimePicker
(even he selects the same date), the microsecond part (including the millisecond) will be gone (becomes 000000
).
Another interesting behavior is that even the control is not specified to present HH:mm:ss
, changing to another date will still preserve the HH:mm:ss
part.
I also tried 3rd party control Infragistics UltraDateTimeEditor
, but according to this, it also only supports precision up to second only.
Hence, my question is that is there any way to make the control with precision up to micro-second instead of second only? Or I must make my own user control to deal with this requirement?
I think i'd make my life easier here and just place a numericupdown visually adjacent to the date time picker, store the microseconds of the source date time in it and add them onto the datetimepicker.Value when the user picks..
dtp.ValueChanged += (sender,args) => {
if(dtp.Tag == "ChangedProgrammatically")
dtp.Tag = null;
else {
dtp.Tag = "ChangedProgrammatically";
dtp.Value = dtp.Value.AddTicks(_microSecNumericUpDown.Value * 10);
}
}
I think setting the value directly also raised the event, hence the "ChangedProgrammatically" code to debounce it but if it doesn't it can be removed
If you want to also provide the user option to edit the microseconds, add an event to its value changed that takes the current dtp value, strips it to just seconds precision and adds them on:
_microsecNud.ValueChanged += (sender,args) => {
dtp.Tag = "ChangedProgrammatically";
dtp.Value = dtp.Value.AddTicks(microsecNud.Value - (dtp.Value.Ticks % TimeSpan.TicksPerSecond));
}
If it all works out, perhaps bundle these two controls into a custom user control..