While adding Delphi 5 compatibility to an older VCL sample, the project failed on a line that is perfectly normal in newer Delphi versions. The code tried to assign a custom date pattern directly to the TDateTimePicker.Format property.
|
1 |
DateTimePicker.Format := 'MM/dd/yyyy'; |
TDateTimePicker is a VCL wrapper around the Windows common control. Newer Delphi versions expose a convenient Format property, but Delphi 5 does not. In Delphi 5 and earlier, the control normally follows the Windows regional date settings unless the underlying common-control message is sent directly.
Use the common-control API for Delphi 5
The compatibility fix is to call DateTime_SetFormat from the CommCtrl unit for Delphi 5, while keeping the direct property assignment for newer compilers. Add CommCtrl to the uses clause when compiling the Delphi 5 branch.
|
1 2 3 4 5 |
{$IFDEF D5} DateTime_SetFormat(DateTimePicker.Handle, PChar('MM/dd/yyyy')); {$ELSE} DateTimePicker.Format := 'MM/dd/yyyy'; {$ENDIF} |
Practical notes
The format string uses the Windows date-time picker syntax, not every Delphi formatting token. Keep the pattern simple and test it on a machine with different regional settings if the application will be deployed internationally.
If you maintain shared sample code across Delphi 5, Delphi 6, and modern Delphi releases, isolate this branch behind a compiler symbol such as D5. That keeps the modern code readable while still documenting the old common-control workaround.
Compatibility checklist
- Add
CommCtrlonly where the Delphi 5 branch needsDateTime_SetFormat. - Pass the picker handle, not the form handle.
- Keep the format string alive for the duration of the call by passing a normal string literal or stable variable.
- Verify the displayed date in the target Windows locale.
Why ShortDateFormat is not enough
Changing Delphi's global date format variables does not reliably change a date-time picker that is backed by the Windows common control. The picker renders according to the control settings and the messages it receives. That is why DateTime_SetFormat is the correct compatibility layer for Delphi 5.
This distinction matters in old sample projects because they are often compiled across several Delphi versions. A line that is clean in Delphi 7 or Delphi 2009 may not exist in Delphi 5, while a Windows common-control message can still be available. Conditional compilation lets each compiler use the most natural path.
Testing old-version compatibility
After applying the workaround, test both the display value and the saved value. The format affects what the user sees; it should not change the underlying TDateTime value used by calculations. That separation is important in purchase orders, invoices, and spreadsheet exports where display formatting and numeric date values must remain consistent.
Also check the form after recreating the window handle. Some VCL controls can lose native-control state if the handle is destroyed and created again. If your application changes styles or parents at runtime, apply the format after the handle is available.
Long-term maintenance note
When a project no longer needs Delphi 5 support, remove the conditional branch and keep the direct Format property assignment. Until then, keep the compatibility code close to the control setup rather than hiding it in unrelated business logic. That makes it clear the workaround exists for a VCL control limitation, not for the purchase-order calculation itself.