?»??»?unit DateMakeEdit; {Rex ­×§ï TMaskEdit 2003/12/04 ¦h³]©w¤F¤@¨ÇÄÝ©Ê DateFormat : Åã¥Ü¤é´Áªº®æ¦¡ ¥»¨Ó·Q³]read noly ¦ý¬O¬°¤F«O¯d ¼u©ÊÁÙ¬O ¨S³] Date : Àx¦sªº¤é´Á DateType : ¤é´Áªº®æ¦¡ dtyyyy = 20030101 dtYY 030101 deEE 920101 ¦h³]even OnDateSet : ·|¦b¤é´Á³Q³]©wÁÙ¦³tEXT ¼g¤J¤é´Á®É³QIJµo } interface uses SysUtils, Classes,Mask; type TDateType = (dtYYYY,dtYY,dtEE); TDateMakeEdit = class(TMaskEdit) private FDate: Double; FDateType: TDateType; FDateFormat: string; FOnDateSet: TNotifyEvent; procedure SetDate(const Value: Double); procedure SetDateType(const Value: TDateType); procedure DoMakeText ; procedure SetDateFormat(const Value: string); function checkDate(DateStr :string ;DateType : TDateType):Boolean; protected { Protected declarations } function Validate(const Value: string; var Pos: Integer): Boolean; override; public { Public declarations } constructor Create(AOwner: TComponent); override; published { Published declarations } property OnDateSet: TNotifyEvent read FOnDateSet write FOnDateSet; property DateFormat : string read FDateFormat write SetDateFormat; property Date:Double read FDate write SetDate; property DateType : TDateType read FDateType write SetDateType; end; procedure Register; implementation uses Dialogs, Controls; procedure Register; begin RegisterComponents('RexSoft', [TDateMakeEdit]); end; { TDateMakeEdit } function TDateMakeEdit.checkDate(DateStr :string ;DateType : TDateType):Boolean; var myy,mmm,mdd : Word ; begin Result := true ; case DateType of //1234567890 dtYYYY :begin //2003/12/31 myy := StrTointDef(copy(DateStr,1,4),0); mmm := StrTointDef(copy(DateStr,6,2),0); mdd := StrTointDef(copy(DateStr,9,2),0); end ; // 123456789 dtYY :begin //03/12/31 myy := StrTointDef(copy(DateStr,1,2),0); if myy < 50 then myy := myy + 2000 else myy := myy + 1900 ; mmm := StrTointDef(copy(DateStr,4,2),0); mdd := StrTointDef(copy(DateStr,7,2),0); end ; // 123456789 dtEE :begin //03/12/31 myy := StrTointDef(copy(DateStr,1,2),0); myy := myy + 1911 ; mmm := StrTointDef(copy(DateStr,4,2),0); mdd := StrTointDef(copy(DateStr,7,2),0); end ; end; try FDate := 0; FDate := EncodeDate(mYY,mMM,mDD); //¥²§K°j°é if Assigned(FOnDateSet) then fOnDateSet(self); except result := False ; MessageDlg('¤é´Á®æ¦¡¤£¹ï!!', mtWarning, [mbOK], 0); abort ; end; end; constructor TDateMakeEdit.Create(AOwner: TComponent); begin inherited; Date := 0 ; DateType := dtEE ; end; procedure TDateMakeEdit.DoMakeText; begin if Date <= 0 then exit ; Text := FormatDateTime(DateFormat,Date); end; procedure TDateMakeEdit.SetDate(const Value: Double); begin if FDate <> Value then begin FDate := Value; DoMakeText ; if Assigned(FOnDateSet) then fOnDateSet(self); end ; end; procedure TDateMakeEdit.SetDateFormat(const Value: string); begin if FDateFormat <> Value then begin FDateFormat := Value; DoMakeText ; end ; end; procedure TDateMakeEdit.SetDateType(const Value: TDateType); begin FDateType := Value; case Value of dtYYYY :begin EditMask := '!0000/00/00;1; '; DateFormat := 'YYYY/MM/DD'; MaxLength := 10 ; end ; dtYY,dtEE :begin MaxLength := 8 ; EditMask :='!00/00/00;1; '; if Value = dtYY then DateFormat := 'YY/MM/DD' else DateFormat := 'EE/MM/DD' ; end ; end; if Width < (MaxLength * 8)+10 then Width := (MaxLength * 8)+11 ; end; function TDateMakeEdit.Validate(const Value: string; var Pos: Integer): Boolean; begin Result := checkDate(value,DateType) ; if not Result then exit ; inherited Validate(Value,Pos); end; end.