The Unofficial Newsletter of Delphi Users - by Robert Vivrette



Formatting a DateTimePicker Control

by Robert Vivrette - RobertV@csi.com

A few days ago, I received an email from a gentleman named Steve who pointed out a failing in the DateTimePicker control. Even though it allows you to show the date in one of two formats (long and short), it did not allow you to change the time format. In Delphi 4, the situation is still not resolved. After a little bit of digging, I figured out a pretty simple way of getting the control to accept a different time format. Although this technique is being used for a DateTimePicker in "Time" display mode, it applies equally well to changing the date format (i.e. to something more than just dtShort and dtLong).

All we are really doing here is sending a message to the control telling it to change its format. This could be wrapped into a descendant control to avoid this obvious hack solution, but since it is just a single line of code (we love those...) I figured I would throw it out there and see what use people find for it.

All you need to do to try out this technique is to drop a TDateTimePicker on a form and add a button. Set the pickers Kind property to dtkTime. Then in the Button's OnClick event, add this line of code:

All this does is send a DTM_SETFORMAT message to the DateTimePicker to tell it to adjust its format. The last parameter (lparam) of the SendMessage call is a pointer to the format you wish to use. In this case, I took a literal string, cast it to a PChar, and converted the PChar to a LongInt (which is what SendMessage ultimately expects). After clicking the button, the format is changed.

There is obviously little error checking here... It is up to you to pass a valid format string, and one that is appropriate to the mode the picker is in. When I set the DateTimePicker to dtkDate (to show the date instead of the time), and then sent a time formatting string, it simply decided to show the time in the format requested rather than the date as the mode indicated.

But... it gets more interesting! You can send essentially any formatting string to the picker control... To see how flexible it was, I used the following format:

As you can see, it is a combination of both date and time formatting commands. The result?

Now you have a DateTimePicker that allows you to manage both the date and time at once. Notice that you can select any portion of the result, for example the month, and by hitting the up and down arrow, the selection will move to the prior month or the next month respectively. Pretty cool!

There are a few drawbacks to this scheme. When you drop down the picker to display the calendar (if you are in dtkDate mode) you obviously will not have the ability to modify the time using the calendar. Same holds true for the reverse.

I am sure someone will take this idea and run with it, perhaps making a new descendant of the TDateTimePicker control that simplifies the method of working with these format strings.

Go To It!

- Robert