{顯示和判斷單元} unit DBDateEditMaskTrans; interface uses Windows, SysUtils, Controls, Forms,Db;
{日期型字段顯示過程,在OnGetText事件中調(diào)用} PRocedure DateFieldGetText(Sender: TField; var Text: String);
{日期型字段輸入判斷函數(shù),在OnSetText事件中調(diào)用} function DateFieldSetText(Sender: TField; const Text: String):Boolean;
implementation
procedure DateFieldGetText(Sender: TField; var Text: String); var dDate:TDate; wYear,wMonth,wDay:Word; aryTestYMD:Array [1..2] of Char ;{測(cè)試輸入掩碼用臨時(shí)數(shù)組} iYMD:Integer; begin dDate:=Sender.AsDateTime; DecodeDate(dDate,wYear,wMonth,wDay); {測(cè)試輸入掩碼所包含的格式.} aryTestYMD:=’年’; if StrScan(PChar(Sender.EditMask), aryTestYMD[1])< >nil then iYMD:=1; aryTestYMD:=’月’; if StrScan(PChar(Sender.EditMask), aryTestYMD[1])< >nil then iYMD:=2; aryTestYMD:=’日’; if StrScan(PChar(Sender.EditMask), aryTestYMD[1])< >nil then iYMD:=3;
case iYMD of 1:{輸入掩碼為:”yyyy年”的格式.} Text:=IntToStr(wYear)+’年’; 2: {輸入掩碼為:”yyyy年mm月”的格式.} Text:=IntToStr(wYear)+’年’+IntToStr(wMonth)+’月’; 3: {輸入掩碼為:”yyyy年mm月dd日”的格式.} Text:=IntToStr(wYear)+’年’+IntToStr(wMonth)+’月’ +IntToStr(wDay)+’日’; else {默認(rèn)為:”yyyy年mm月dd日”的格式.} Text:=IntToStr(wYear)+’年’+IntToStr(wMonth)+’月’ +IntToStr(wDay)+’日’; end;
end;
function DateFieldSetText(Sender: TField; const Text: String):Boolean; var dDate:TDate; sYear,sMonth,sDay:String; aryTestYMD:Array [1..2] of Char; iYMD:Integer; begin {獲得用戶輸入的日期} sYear:=Copy(Text,1,4); sMonth:=Copy(Text,7,2); SDay:=Copy(Text,11,2);
{測(cè)試輸入掩碼所包含的格式.} aryTestYMD:=’年’; if StrScan(PChar(Sender.EditMask), aryTestYMD[1])< >nil then iYMD:=1; aryTestYMD:=’月’; if StrScan(PChar(Sender.EditMask), aryTestYMD[1])< >nil then iYMD:=2; aryTestYMD:=’日’; if StrScan(PChar(Sender.EditMask), aryTestYMD[1])< >nil then iYMD:=3;
{利用Try…Except進(jìn)行輸入的日期轉(zhuǎn)換} try begin case iYMD of 1: {輸入掩碼為:”yyyy年”的格式.} begin dDate:=StrToDate(sYear+’-01-01’) ;{中文Windows默認(rèn)的日期格式為:yyyy-mm-dd.下同} Sender.AsDateTime:=dDate; end; 2: {輸入掩碼為:”yyyy年mm月”的格式.} begin dDate:=StrToDate(sYear+’-’+sMonth+’-01’); Sender.AsDateTime:=dDate; end; 3: {輸入掩碼為:”yyyy年mm月dd日”的格式.} begin dDate:=StrToDate(sYear+’-’+sMonth+’-’+sDay); Sender.AsDateTime:=dDate; end; else {默認(rèn)為:”yyyy年mm月dd日”的格式.} begin dDate:=StrToDate(sYear+’-’+sMonth+’-’+sDay); Sender.AsDateTime:=dDate; end; end; DateFieldSetText:=True; end; except {日期轉(zhuǎn)換出錯(cuò)} begin application.MessageBox(PChar(Text+’不是有效的日期!’), ’錯(cuò)誤’,mb_Ok+mb_IconError); DateFieldSetText:=False; end; end;
end;
end.
{主窗口單元} unit Main;
interface
uses ……{略去其他內(nèi)容} procedure Table1BirthdayGetText(Sender: TField; var Text: String;DisplayText: Boolean); procedure Table1BirthdaySetText(Sender: TField; const Text: String); private { Private declarations } public { Public declarations } ……{略} implementation
procedure TForm1.Table1BirthdayGetText(Sender: TField; var Text: String;DisplayText: Boolean); begin DateFieldGetText(Sender,Text); end;
procedure TForm1.Table1BirthdaySetText(Sender: TField; const Text: String); begin if DateFieldSetText(Sender,Text)=False then Abort; {轉(zhuǎn)換不成功,日期非法} end;