我这儿有一个控件,可以设定日期显示的格式,是从TCustomEdit继承而来,
可以实现你需要的功能.
源代码如下:
//用于输入Date数据的Edit控件, 添加了以下属性:
// Alignment: taLeftJustify, taRightJustify, taCenter. 指定对齐方式.
// DateValue: 用于读取/设定Edit控件的Date数据.
// ErrorMessage: 当输入错误时MessageBox的提示信息.
// ErrorTitle: 当输入错误时MessageBox的标题.
// Format: 用于控制Date显示的格式,Format格式见FormatDateTime函数的帮助.
unit DateEdit;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, StdCtrls, Graphics;
type
TDateEdit = class(TCustomEdit)
private
{ Private declarations }
FAlignment: TAlignment;
FDate: TDate;
FErrorMessage: String;
FErrorTitle: String;
FFormat: String;
//用于控制Date显示的格式,Format格式见FormatDateTime函数的帮助.
protected
{ Protected declarations }
procedure CreateParams(var Params: TCreateParams); override;
procedure DoExit; override;
function GetAlignment: TAlignment; virtual;
function GetDate: TDate; virtual;
procedure SetAlignment(NewAlignment: TAlignment); virtual;
procedure SetDate(NewDate: TDate); virtual;
procedure SetFormat(NewFormat: String); virtual;
property CharCase;
property MaxLength;
property PasswordChar;
property Text;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ Published declarations }
property Alignment: TAlignment read GetAlignment write SetAlignment;
property Anchors;
property AutoSelect;
property AutoSize;
property BevelEdges;
property BevelInner;
property BevelKind default bkNone;
property BevelOuter;
property BiDiMode;
property BorderStyle;
property Color;
property Constraints;
property Ctl3D;
property DateValue: TDate read GetDate write SetDate;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property ErrorMessage: String read FErrorMessage write FErrorMessage;
property ErrorTitle: String read FErrorTitle write FErrorTitle;
property Font;
property Format: String read FFormat write SetFormat;
property HideSelection;
property ImeMode;
property ImeName;
property OEMConvert;
property ParentBiDiMode;
property ParentColor;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ReadOnly;
property ShowHint;
property TabOrder;
property TabStop;
property Visible;
property OnChange;
property OnClick;
property OnContextPopup;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDock;
property OnStartDrag;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Standard', [TDateEdit]);
end;
procedure TDateEdit.CreateParams(var Params: TCreateParams);
const
Alignments: array[TAlignment] of WORD = (ES_LEFT, ES_RIGHT, ES_CENTER);
begin
inherited CreateParams(Params);
Params.Style := Params.Style or Alignments[FAlignment];
end;
procedure TDateEdit.DoExit;
var
TempDateTime: TDateTime;
TempColor: TColor;
TempString: String;
Index: Integer;
begin
inherited DoExit;
TempString := Text;
Index := 1;
while (Index <= Length(TempString)) and (TempString[Index] >= '0') and
(TempString[Index] <= '9') do Inc(Index);
if Index > Length(TempString) then
begin
case Length(TempString) of
6 : TempString := Copy(TempString, 1, 2) + '-' + Copy(TempString, 3, 2) + '-' + Copy(TempString, 5, 2);
8 : TempString := Copy(TempString, 1, 4) + '-' + Copy(TempString, 5, 2) + '-' + Copy(TempString, 7, 2);
end;
end
else begin
for Index := 1 to Length(TempString) do
if (TempString[Index] < '0') or (TempString[Index] > '9') then
TempString[Index] := '-';
end;
if not TryStrToDateTime(TempString, TempDateTime) then
begin
TempColor := Font.Color;
Font.Color := clRed;
MessageBox(0, PChar(FErrorMessage), PChar(FErrorTitle), MB_ICONINFORMATION + MB_OK);
Font.Color := TempColor;
SetFocus;
end
else DateValue := TempDateTime;
end;
function TDateEdit.GetAlignment: TAlignment;
begin
GetAlignment := FAlignment;
end;
function TDateEdit.GetDate: TDate;
begin
Result := FDate;
end;
procedure TDateEdit.SetAlignment(NewAlignment: TAlignment);
begin
if FAlignment <> NewAlignment then
begin
FAlignment := NewAlignment;
if not (csLoading in componentstate) then ReCreateWnd;
end;
end;
procedure TDateEdit.SetDate(NewDate: TDate);
begin
FDate := NewDate;
Text := FormatDateTime(FFormat, FDate);
end;
procedure TDateEdit.SetFormat(NewFormat: String);
begin
if FFormat <> NewFormat then
begin
FFormat := NewFormat;
DateValue := FDate;
end;
end;
constructor TDateEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FAlignment := taLeftJustify;
DateValue := Now;
FErrorMessage := 'Invalid Date Type.';
FErrorTitle := 'Input Error:';
Format := 'yyyy''/''mm''/''dd';
end;
destructor TDateEdit.Destroy;
begin
inherited Destroy;
end;
end.