by Steve Gill - cybercom@netinfo.com.au
Editors Note: In response to the article run earlier this month on Formatting the Time format in the TDateTimePicker component, Steve has wrapped the whole thing into a descendant class of TDateTimePicker.
I have had a go at putting a time format into a descendant component of TDateTimePicker (see attached zip file). Another shortcoming of the TDateTimePicker component is that it has no TabOrder property. This was a real barrel of laughs until I finally realised why I couldn't get the tab order to work correctly. Every time I set the tab order of the components on a form, the TDateTimePicker would default back to whatever it's setting was. I have added the TabOrder property to the component as well.
unit DateTimeEdit;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics,
Controls, Forms, Dialogs,
ComCtrls;
type
// TTimeFormat
TTimeFormatKind = (tfSeconds, tfAMPM, tfTwoDigitHour);
TTimeFormat = set of TTimeFormatKind;
// TDateTimeEdit
TDateTimeEdit = class(TDateTimePicker)
private
{ Private declarations }
FTimeFormat : TTimeFormat;
procedure SetTimeFormat(Value
: TTimeFormat);
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner : TComponent);
override;
published
{ Published declarations }
property TabOrder;
property TimeFormat : TTimeFormat
read FTimeFormat
write SetTimeFormat;
end;
procedure Register;
implementation
{$R *.RES}
// TDateTimeEdit
constructor TDateTimeEdit.Create(AOwner : TComponent);
begin
inherited Create(AOwner);
// Set default time format
FTimeFormat := [tfSeconds,
tfTwoDigitHour, tfAMPM];
end;
procedure Register;
begin
RegisterComponents('Win32',
[TDateTimeEdit]);
end;
procedure TDateTimeEdit.SetTimeFormat(Value : TTimeFormat);
var
MyTimeFormat : string;
begin
if FTimeFormat <> Value
then
begin
FTimeFormat := Value;
// Do you want one or two digit hours?
if tfTwoDigitHour in FTimeFormat then MyTimeFormat := 'hh:mm'
else MyTimeFormat := 'h:mm';
// Do you want seconds?
if tfSeconds in FTimeFormat then MyTimeFormat := MyTimeFormat + ':ss';
// Do you want AM/PM?
if tfAMPM in FTimeFormat then MyTimeFormat := MyTimeFormat + ' tt';
// Set the format
SendMessage(Handle, $1005, 0, Longint(PChar(MyTimeFormat)));
Invalidate;
end;
end;
end.