在Delphi中處理資料庫日期型欄位的顯示與輸入

來源:互聯網
上載者:User

使用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:I ger;
begin
   dDate:=Sender.AsDateTime;
   DecodeDate(dDate,wYear,wMonth,wDay);
{測試輸入遮罩所包含的格式.}
   aryTestYMD:=’年’;
   if StrScan(PChar(Sender.EditMask),
   aryTestYMD[1])$#@60; $#@62;nil then
   iYMD:=1;
   aryTestYMD:=’月’;
   if StrScan(PChar(Sender.EditMask),
   aryTestYMD[1])$#@60; $#@62;nil then
   iYMD:=2;
   aryTestYMD:=’日’;
   if StrScan(PChar(Sender.EditMask),
   aryTestYMD[1])$#@60; $#@62;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])$#@60; $#@62;nil then
   iYMD:=1;
   aryTestYMD:=’月’;
if StrScan(PChar(Sender.EditMask),
   aryTestYMD[1])$#@60; $#@62;nil then
   iYMD:=2;
   aryTestYMD:=’日’;
if StrScan(PChar(Sender.EditMask),
   aryTestYMD[1])$#@60; $#@62;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.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.