Author: Lou Adler
A Better TDateTimePicker
Answer:
Although the TDateTimePicker component is quite useful sometimes (I still prefer my
own date-picking components), it lacks an important feature: the ability to set
your own date/time format, or even a mix of both. It's restricted to the two
Windows date/time format: the short and the long format. With that on my mind, I
wrote a TDateTimePicker descendant, TASDateTimePickerEx that will allow you to
change the format of the component to whatever you want. Without further delay,
here's the code for the new component:
1 unit ASDTPickEx;
2
3 interface
4
5 uses
6 Windows, Classes, ComCtrls;
7
8 type
9 TASDateTimePickerEx = class(TDateTimePicker)
10 private
11 FFormatStr: string;
12 procedure SetFormatStr(AValue: string);
13 { Private declarations }
14 protected
15 { Protected declarations }
16 public
17 constructor Create(AOwner: TComponent); override;
18 { Public declarations }
19 published
20 property FormatString: string read FFormatStr
21 write SetFormatStr;
22 { Published declarations }
23 end;
24
25 procedure register;
26
27 implementation
28
29 uses CommCtrl;
30
31 procedure register;
32 begin
33 RegisterComponents('New', [TASDateTimePickerEx]);
34 end;
35
36 constructor TASDateTimePickerEx.Create(AOwner: TComponent);
37 begin
38 inherited;
39 FFormatStr := 'MM/dd/YYYY';
40 end;
41
42 procedure TASDateTimePickerEx.SetFormatStr(AValue: string);
43 begin
44 try
45 Perform(DTM_SETFORMAT, 0, longint(pchar(AValue)));
46 FFormatStr := AValue;
47 except
48 Perform(DTM_SETFORMAT, 0, longint(pchar(FFormatStr)));
49 end;
50 end;
51
52 end.
After installing the component (it has been tested on both Delphi 3 and 4), you can
play with the FormatString property, by assigning it values like 'ddd,
MMMM/dd/yyyy', or whatever you want. I mean, you can create whatever format you
want for either the date or the time.
Download source
code
http://www.bhnet.com.br/~simonet/archive/asdtpickex.pas
|