Display and input process database date fields in Delphi

      delphi conduct database design, inevitably involve the input of the issue date fields. However, compared with the Chinese version of Microsoft Access 97, etc., display and date fields of input methods provided by Delphi itself is not suitable for Chinese people's habits. So for handling date-type field, we made a lot of solutions, but the processing results are not unified on the display and input, for example, can be achieved format "yyyy Year mm Month dd Day" is displayed, but still want input in accordance with the practice of using foreign form "yyyy-mm-dd" input; TdateTimePicker used to select the trouble to enter the total; there are ways to modify some settings but also the properties of the system, thus making software release property when you want to carry out system adjustment; the use of third-party control is also a way to control packaged and released. And for commonly used to "1999", "November 1999" and the date format, there is no corresponding processing. Here I according to their own practice, the use of combined OnGetText and OnSetText two events TField, in order to achieve a unified display and input date fields, and can deal with our common "1999", "November 1999" and other dates display and input form, all using the event Delphi implementation provided, without modifying any system settings. After corresponding expansion, it may also be used to display and input time, such as "hh mm dot points" and the like. At the same time, because it is directly controlled TField events, so no matter TDBGrid use or use TDBEdit, are normally carried out centrally, without having to be considered separately. Similar procedure can also be applied to the input of non-date database application. 

1 the basic idea of 

  using the EditMask TField properties, both as to its display and input masks, processing date field displayed in OnGetText TField event, the effectiveness of the process determines whether the input value in the event OnSetText. In order to reuse the code, and the OnGetText OnSetText event processes and functions into a separate unit procedure calls. 

2 Specific implementation code 

{display and determination unit}
Unit DBDateEditMaskTrans;
interface
uses
the Windows , the SysUtils, Controls, Forms, Db;

{date fields display process, called OnGetText event}
Procedure DateFieldGetText (Sender: of TField; var the Text: String) ;

{date fields input judging function, called OnSetText event}
function DateFieldSetText (Sender: of TField; const the Text: String): Boolean;

Implementation

Procedure DateFieldGetText (Sender: of TField; var the Text: String);
var
  dDate: tdate;
  wYear , wMonth, WDAY: Word;
  aryTestYMD: the array [1..2] of Char; { test input mask with the temporary array}
  iYMD: the I GER;
the begin
  dDate: = Sender.AsDateTime;
  DecodeDate (dDate, wYear, wMonth, WDAY );
{testing included the input mask format.}
  aryTestYMD: = 'on';
  IF StrScan (the PChar (Sender.EditMask),
  aryTestYMD [. 1]) @ $ # 60; # @ $ 62 is; nil the then
  iYMD: =. 1;
  aryTestYMD: = 'month';
  IF StrScan (the PChar (Sender.EditMask),
  aryTestYMD [. 1]) @ $ # 60; # @ $ 62 is; nil the then
  iYMD: = 2;
  aryTestYMD: = 'Japanese';
  IF StrScan (the PChar (Sender.EditMask),
  aryTestYMD [. 1]) 60 @ # $; @ # $ 62 is; nil the then
  iYMD: =. 3;

Case of iYMD
 . 1: {input mask as: "YYYY year" format.}
  the Text: = the IntToStr (wYear) + 'on';
 2: {input mask as: "YYYY Year month mm" format.}
  the Text: = the IntToStr (wYear) + 'of' + IntToStr (wMonth) + 'month';
 3: {input mask as: "YYYY Year mm months dd day. "format}
  the Text: = the IntToStr (wYear) + 'of' + IntToStr (wMonth) + 'month' + IntToStr (wDay) + 'day';
 the else {default:" yyyy mm-dd-day " } format.
  Text: = IntToStr (wYear) + ' of' + IntToStr (wMonth) + 'month' + IntToStr (wDay) + 'day';
End;

End;

function DateFieldSetText (Sender: TField; const Text: String): Boolean;
var
  dDate: tdate;
  sYear, sMonth, SDAY: String;
  aryTestYMD: the Array [1..2] of Char;
  iYMD: Integer;
the begin
{get user input date}
  sYear: = the Copy (the Text, l, 4);
  sMonth: the Copy = (the Text, 7,2);
  SDAY: = the Copy (the Text, 11,2);

{. formats included in the test input masks}
  aryTestYMD: = 'on';
IF StrScan (the PChar (Sender.EditMask),
  aryTestYMD [. 1]) @ $ # 60; # @ $ 62 is; nil the then
  iYMD: =. 1;
  aryTestYMD: = 'month';
IF StrScan (the PChar (Sender.EditMask),
  aryTestYMD [. 1]) # @ $ 60; $ @ 62 is #; nil the then
  iYMD: = 2;
  aryTestYMD: = 'Japanese';
IF StrScan (the PChar (Sender.EditMask),
  aryTestYMD [. 1]) @ $ # 60; # @ $ 62 is; nil the then
  iYMD: =. 3;

{use Try ... Except for converting input date}
the try
the begin
Case of iYMD
 . 1: {input mask as: "yyyy year" format.}
  the begin
   dDate: = StrToDate (sYear + '- 01-01'); {Chinese Windows default date format is: yyyy-mm-dd hereinafter the same}
   Sender.AsDateTime: = dDate;
  End;
 2: {input mask as: "yyyy mm on month" format}.
  the begin
   dDate: = StrToDate (sYear + '-' + sMonth + '- 01');
   Sender .AsDateTime: = dDate;
  End;
 . 3: {input mask as: format "yyyy mm-dd-day".}
  the begin
   dDate: = StrToDate (sYear + '-' + sMonth + '-' + SDAY);
   Sender.AsDateTime : = dDate;
  End;
 the else {default: format "yyyy mm-dd-day".}
 the begin
  dDate: = StrToDate (sYear + '-' + sMonth + '-' + SDAY);
  Sender.AsDateTime: = dDate;
 End;
 End;
  DateFieldSetText: = True;
 End;
 the except
{date conversion error}
the begin
 Application.MessageBox (the PChar ( Text + '! not a valid date'), 'error', MB_OK MB_ICONERROR +);
 DateFieldSetText: = False;
End;
End;

End;

. End

{main window unit}
unit the main;

interface

uses
...... {omit other content}
Table1BirthdayGetText Procedure (Sender: of TField; the Text var: String; the DisplayText: Boolean);
Procedure Table1BirthdaySetText (Sender: of TField; const the Text: String);
Private
{Private Declarations}
public
Declarations} Public {
...... {omitted}
Implementation

{custom unit comprising come}
uses DBDateEditMaskTrans;

{$ R & lt *. DFM}
...... other steps are omitted {}
Procedure TForm1.FormActivate (Sender: TObject);
{set a date input mask type field can be placed TField field definition. }
The begin
  Table1.FieldByName ( 'Birthday') an EditMask:. = '9999 \ In 99 \ dated 99 \ day;. 1; _';
End;

Procedure TForm1.Table1BirthdayGetText (Sender: of TField; the Text var: String; the DisplayText: Boolean) ;
the begin
  DateFieldGetText (Sender, the Text);
End;

Procedure TForm1.Table1BirthdaySetText (Sender: of TField; const the Text: String);
the begin
  IF DateFieldSetText (Sender, the Text) = False the then
  the Abort; {conversion is unsuccessful, the illegal date}
End;

End .

Guess you like

Origin www.cnblogs.com/jijm123/p/12406081.html