在使用Delphi進行資料庫設計時,不可避免的會涉及日期型欄位的輸入問題。不過與Microsoft的access 97中文版等相比,Delphi本身提供的日期型欄位的顯示與輸入方式並不適合中國人的習慣。因此對於日期型欄位的處理,大家提出了不少解決方法,但是處理結果在顯示和輸入上並不統一,例如顯示時可以實現“yyyy年mm月dd日”的格式,但是在輸入時還是要依照國外的習慣以「yyyy-mm-dd」的形式進行輸入;而使用TdateTimePicker進行選擇輸入總嫌麻煩;有些方法還要修改系統的一些設定屬性,因而在進行軟體發佈時要將系統的屬性進行調整;採用第三方控制的方式則也要將控制項打包發佈。而且對於常用到的「1999年」、「1999年11月」等日期格式,並沒有進行對應的處理。這裡我根據自己的實踐,利用TField的OnGetText和OnSetText兩個事件的結合,以期達到日期型字段的顯示和輸入的統一,並可以處理我們常見的“1999年”、“1999年11月”等日期形式的顯示與輸入,全部利用Delphi提供的事件實現,不需要修改任何系統設定。進行相應的擴展後,還可以用於時間的顯示和輸入,如“hh點mm分”等。同時,由於是直接控制TField的事件,所以不論使用TDBGrid還是用TDBEdit,都可以正常的進行統一處理,而不必分開考慮。採用類似的方法,也可以應用於非資料庫應用程式中的日期輸入。
1 基本思想
利用TField的EditMask屬性,將其同時作為顯示和輸入的掩碼,在TField的OnGetText事件中處理日期欄位的顯示,而在OnSetText事件中處理輸入值的有效性判斷。為了重複利用程式碼,將OnGetText和OnSetText的事件處理過程呼叫的過程和函數放到一個獨立的單元。
2 具體實作程式碼
{顯示與判斷單元}
unit DBDateEditMaskTrans;
interface
uses
Windows, SysUtils, Controls, Forms,Db;
{日期型欄位顯示過程,在OnGetText事件中呼叫}
PRocedure DateFieldGetText(Sender: TField; var Text: String);
{日期型欄位輸入判斷函數,在OnSetText事件中呼叫}
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 ;{測試輸入掩碼用臨時數組}
iYMD:Integer;
begin
dDate:=Sender.AsDateTime;
DecodeDate(dDate,wYear,wMonth,wDay);
{測試輸入遮罩所包含的格式.}
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 {預設為:”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);
{測試輸入遮罩所包含的格式.}
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進行輸入的日期轉換}
try
begin
case iYMD of
1: {輸入遮罩為:”yyyy年」的格式.}
begin
dDate:=StrToDate(sYear+'-01-01') ;{中文Windows預設的日期格式為: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 {預設為:”yyyy年mm月dd日”的格式.}
begin
dDate:=StrToDate(sYear+'-'+sMonth+'-'+sDay);
Sender.AsDateTime:=dDate;
end;
end;
DateFieldSetText:=True;
end;
except
{日期轉換出錯}
begin
application.MessageBox(PChar(Text+'不是有效的日期!'), '錯誤',mb_Ok+mb_IconError);
DateFieldSetText:=False;
end;
end;
end;
end.
{主視窗單元}
unit Main;
interface
uses
……{略去其他內容}
procedure Table1BirthdayGetText(Sender: TField; var Text: String;DisplayText: Boolean);
procedure Table1BirthdaySetText(Sender: TField; const Text: String);
private
{ Private declarations }
public
{ Public declarations }
……{略}
implementation
{將自訂的單元包含進來}
uses DBDateEditMaskTrans;
{$R *.DFM}
……{其他過程略}
procedure TForm1.FormActivate(Sender: TObject);
{設定一個日期型欄位的輸入掩碼,可以放到TField欄位定義中。 }
begin
Table1.FieldByName('Birthday').EditMask:= '9999年99月99日;1;_';
end;
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; {轉換不成功,日期非法}
end;
end.