给你一个日期控件:
unit SPECDateEdit;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Mask;
type
TDateValidCheck = procedure (Sender: TObject) of object;
type
TSPECDateEdit = class(TMaskEdit)
private
{ Private declarations }
FFontColor: TColor;
FColor: TColor;
FValid :TDateValidCheck; // 日期有效性判断
FValue :TDate; // 当前日期值
FCanEmpty :Boolean; // 是否必须输入日期
FIsEmpty :Boolean; // 没有指定值
FDateFormat:String; // 使用的日期格式
protected
{ Protected declarations }
procedure setValue(dInDate:TDate);
function getValue:TDate;
procedure KeyPress( var Key: Char );override;
procedure setEnabled(pEnabled:Boolean);override;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
procedure ValidateEdit; override;
published
{ Published declarations }
property Width default 70;
property DateFormat:String read FDateFormat write FDateFormat;
property Value:TDate read getValue write setValue;
property CanEmpty:Boolean read FCanEmpty write FCanEmpty default true;
property isEmpty:Boolean read FisEmpty write FisEmpty;
property OnValid:TDateValidCheck read FValid write FValid;
end;
implementation
constructor TSPECDateEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
self.FColor :=self.Color;
self.FFontColor :=self.Font.Color;
Self.MaxLength := 10;
Self.EditMask := '0000-00-00;1; ';
Self.Font.Name := '宋体';
Self.Font.Size := 9;
Self.Width := 70;
Self.DateFormat := 'YYYY-MM-DD';
Self.CanEmpty := true;
end;
procedure TSPECDateEdit.setEnabled(pEnabled:Boolean);
begin
inherited;
if self.Ctl3D=true then begin
if pEnabled=false then begin
self.Color:=clBtnFace;
self.Font.Color:=clBlue;
end else begin
self.Color:=FColor;
self.Font.Color:=FFontColor;
end;
end;
end;
procedure TSPECDateEdit.KeyPress( var Key: Char );
begin
if key = #13 then begin
GetParentForm(self).Perform(WM_NEXTDLGCTL,0,0);
Key:=#0;
end;
inherited;
end;
function TSPECDateEdit.getValue:TDate;
begin
result := Self.FValue;
end;
procedure TSPECDateEdit.setValue(dIndate:TDate);
begin
Self.FValue:= dIndate;
Self.Text := FormatDateTime(Self.DateFormat,dIndate);
end;
procedure TSPECDateEdit.ValidateEdit;
var
dDate :TDate;
sOldStr :String;
begin
Self.isEmpty := true;
if Self.CanEmpty=false or not (trim(Self.Text)='- -') then begin
sOldStr := ShortDateFormat;
ShortDateFormat := Self.DateFormat;
try
dDate := StrToDate(Self.text);
Self.FValue := dDate;
Self.isEmpty := false;
except
on EConvertError do Self.SetFocus;
end;
end;
if Self.CanEmpty=true and (trim(Self.Text)='- -') then Self.FValue:=0;
if assigned(FValid) then OnValid(Self);
end;
end.