The display and input of database date-type fields in Delphi processing

Source: Internet
Author: User

When using Delphi for database design, it is inevitable that the input problem of date type fields will be involved. However, compared with Microsoft Access 97 Chinese version, the format of the date-type fields provided by Delphi is not suitable for Chinese custom.

So for the processing of date-type fields, we have put forward a lot of solutions, but the results in the display and input is not uniform, such as display can be achieved "yyyy mm month DD Day" format, but in the input or in accordance with foreign habits in the form of "YYYY-MM-DD" input While using Tdatetimepicker to select input is always troublesome; some methods also modify some of the system's settings properties, so that the system's properties are adjusted when the software is published, and the controls are packaged and published in the same way as third party controls. And for the commonly used "1999", "November 1999" date format, no corresponding treatment. Here I based on my own practice, using the combination of Tfield Ongettext and Onsettext two events, in order to achieve the display of date-type fields and the unification of the input, and can handle our common "1999", "November 1999" and other date forms of display and input, all Using the event implementation provided by Delphi, you do not need to modify any system settings. After the corresponding expansion, can also be used for time display and input, such as "HH point mm points" and so on. At the same time, because it is the direct control of Tfield events, so whether the use of Tdbgrid or tdbedit, can be normal treatment, without separate consideration. A similar approach can also be applied to date input in non-database applications.

1 Basic Ideas

Using Tfield's Editmask attribute, it acts as a display and input mask, handles the display of date fields in the Tfield Ongettext event, and handles the validity of input values in the Onsettext event. To reuse the code, the procedures and functions invoked by the Ongettext and Onsettext event-handling procedures are placed in a separate unit.

2 Specific implementation code

{Display and Judgment unit}
Unit Dbdateeditmasktrans;
Interface
Uses
Windows, Sysutils, Controls, forms,db;
{Date-Type field display procedure, calling in Ongettext event}
Procedure Datefieldgettext (Sender:tfield; var text:string);
{Date field input judgment function, called in Onsettext event}
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; {test input mask with temporary array}
Iymd:i ger;
Begin
Ddate:=sender.asdatetime;
Decodedate (Ddate,wyear,wmonth,wday);
{Test the format that the input mask contains.}
arytestymd:= ' year ';
If Strscan (Pchar (Sender.editmask),
ARYTESTYMD[1]) $#@60; $#@62;nil Then
Iymd:=1;
arytestymd:= ' Month ';
If Strscan (Pchar (Sender.editmask),
ARYTESTYMD[1]) $#@60; $#@62;nil Then
iymd:=2;
Arytestymd:= ' Day ';
If Strscan (Pchar (Sender.editmask),
ARYTESTYMD[1]) $#@60; $#@62;nil Then
iymd:=3;
Case Iymd of
1:{Input mask is: "yyyy year" format.}
Text:=inttostr (wyear) + ' year ';
2: {The input mask is: "YYYY year mm Month" format.}
Text:=inttostr (wyear) + ' year ' +inttostr (wmonth) + ' month ';
3: {The input mask is: "YYYY year mm month DD Day" format.}
Text:=inttostr (wyear) + ' year ' +inttostr (wmonth) + ' month ' +inttostr (wday) + ' Day ';
else {The default is: "YYYY year mm month DD Day" format.}
Text:=inttostr (wyear) + ' year ' +inttostr (wmonth) + ' month ' +inttostr (wday) + ' Day ';
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
{Get the date entered by the user}
Syear:=copy (text,1,4);
Smonth:=copy (text,7,2);
Sday:=copy (text,11,2);
{Test the format that the input mask contains.}
arytestymd:= ' year ';
If Strscan (Pchar (Sender.editmask),
ARYTESTYMD[1]) $#@60; $#@62;nil Then
Iymd:=1;
arytestymd:= ' Month ';
If Strscan (Pchar (Sender.editmask),
ARYTESTYMD[1]) $#@60; $#@62;nil Then
iymd:=2;
Arytestymd:= ' Day ';
If Strscan (Pchar (Sender.editmask),
ARYTESTYMD[1]) $#@60; $#@62;nil Then
iymd:=3;
{Use Try ... Except date conversion for input}
Try
Begin
Case Iymd of
1: {The input mask is: "yyyy year" format.}
Begin
Ddate:=strtodate (syear+ '-01-01 '); {The default date format for Windows in Chinese is: YYYY-MM-DD.}
Sender.asdatetime:=ddate;
End
2: {The input mask is: "YYYY year mm Month" format.}
Begin
Ddate:=strtodate (syear+ '-' +smonth+ '-01 ');
Sender.asdatetime:=ddate;
End
3: {The input mask is: "YYYY year mm month DD Day" format.}
Begin
Ddate:=strtodate (syear+ '-' +smonth+ '-' +sday);
Sender.asdatetime:=ddate;
End
else {The default is: "YYYY year mm month DD Day" format.}
Begin
Ddate:=strtodate (syear+ '-' +smonth+ '-' +sday);
Sender.asdatetime:=ddate;
End
End
Datefieldsettext:=true;
End
Except
{Date Conversion Error}
Begin
Application.messagebox (Pchar (text+ ' is not a valid date! '), ' error ', mb_ok+mb_iconerror);
Datefieldsettext:=false;
End
End
End
End.
{main Window cell}
Unit Main;
Interface
Uses
...... {Omit other content}
Procedure Table1birthdaygettext (Sender:tfield; var text:string;displaytext:boolean);
Procedure Table1birthdaysettext (Sender:tfield; const text:string);
Private
{Private declarations}
Public
{Public declarations}
...... Slightly
Implementation
{Include custom cells in}
Uses Dbdateeditmasktrans;
{$R *. DFM}
...... {Other procedures are slightly}
Procedure Tform1.formactivate (Sender:tobject);
{Set an input mask for a date field that can be placed in the Tfield field definition.} }
Begin
Table1.fieldbyname (' birthday '). Editmask:= ' 9999\ year 99\ month 99\ day; 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; {The conversion was unsuccessful, the date is illegal}
End
End.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.