程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 在Delphi中處理數據庫日期型字段的顯示與輸入

在Delphi中處理數據庫日期型字段的顯示與輸入

編輯:Delphi

使用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.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved