VisualHint's blog

The ten deceptions of the DateTimePicker

If you are here reading these lines it’s because you have something against the .Net DateTimePicker. And you’re not alone since, if you do a search in the newsgroups, the same questions keep coming: “how do I set the background color of the DTP?”, or “it does not accept a null value and my DateTime column in my database has some empty cells”.

CodeProject and other similar developer communities give you a lot of pseudo solutions to solve one or two issues of the control… when they don’t create new problems. They start with deriving from the DateTimePicker class and overriding certain methods and end with the magic textbox displayed in front to hide the date value when null is the only thing desired. So it’s obvious that everyone complains about the DateTimePicker shortcomings and everyone needs a solution. What are the problems of this component which is, don’t forget it, the underlying win32 SysDateTimePick32 window class, nothing more:

  1. The complaint number one is that it doesn’t accept a null or DBNull value. This is critical when binding it to a nullable DateTime column in a database. Consequently it’s not possible to display a custom string when this value is null.
  2. It suffers some display issues: when closing the calendar, the focus cue disappears, except if a checkbox is visible, in which case this is the one taking the focus. Speaking of the checkbox, it will sometimes show this cue even when the control itself is not focused. No mouse hover effect is seen on the dropdown button and checkbox, however there is one for the updown button. And don’t ask me why you can’t show an updown button with a dropdown button at the same time…
  3. There are also some inconsistencies compared to the usual combobox and checkbox controls. When clicking those buttons, the corresponding action is triggered on mouse down instead of being triggered on mouse up.
  4. If you disable the date by clicking on the checkbox, which is by the way ridiculously small under a non xp theme, clicking the dropdown button instantly reenables it.
  5. There is no text formatting options except for the month calendar. The background color can’t be set.
  6. Programmatically you can’t control which field is the selected one.
  7. This is the same for the dropdown calendar. You won’t be able to show it or hide it without the user intervention.
  8. Some people would like a way to automatically jump to the next editable field when all characters have been typed in the current field instead of having to type the right arrow key each time.
  9. Where are callback fields? Do you remember the DTN_FORMATQUERY and DTN_FORMAT messages? It was great to create custom fields but this feature is not natively present in the .net control.
  10. No custom culture can be applied on the displayed string. The Microsoft KB says it clearly: you can do it only by changing the current culture in the control panel. Pretty annoying for applications that want to change this regional setting on the fly, usually when choosing a language in a preferences box.

There are some more issues that I had, specifically in Smart PropertyGrid.Net. Among the built-in inplace controls this was the only one to vertically align the text at the top. No way to change that. When a property expands on multiple rows or simply increases its vertical margin this is not very nice. Also, a field calculates its width based on the largest string it can accept (“September” for the month for example). This was also an issue with SPG. When the inplace control was not active, I had no way to display the text with the same spacing between fields and therefore when selecting the datetime property, all the fields were shifting in a non pleasant way.

I guess there are some more concerns that you encountered while coping with the DateTimePicker. You can add them in the comments so that we have a good starting list to know what else needs to be fixed.