在Delphi中处理数据库日期型字段的显示与输入
来源:岁月联盟
时间:2009-05-31
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; {转