Processing the display and input of database date fields in Delphi

zhaozj2021-02-11  223

When using Delphi for database design, it is inevitable to involve input issues for the date field. However, compared with Microsoft Access 97 Chinese version, the date-type field provided by Delphi itself is not suitable for Chinese habits. Therefore, for the processing of the date field, everyone puts forward a lot of solutions, but the resulting result is not uniform on display and input, such as displaying "YYYY MM Moon DD Day" format, but is still In accordance with the habit of "YYYY-mm-DD", use TDATETIMEPICKER to select the input, you will have a problem; some methods also modify some of the system's setting properties, so the properties of the system should be made when the software is published. Adjustment; use third-party controls to packet the control package. Moreover, there is no corresponding treatment for the date format commonly used "1999", "November 1999". Here I use the combination of TField's OnGetText and ONSETTEXT, in order to achieve the unity of the date field, and can handle our common "1999", "November 1999" and so on. The formal display and input, all of which is implemented using the event provided by Delphi, and does not need to modify any system settings. After performing the corresponding expansion, it can also be used for time display and input, such as "HH point mm points", etc. At the same time, since it is directly controlled TField event, no matter whether it is using TDBGRID or use TDBEDIT, it can be normalized, without having to consider it separately. Similar methods can also be applied to date input in non-agricultural applications.

1 basic idea

With the TFIELD's EditMask property, simultaneously use the mask displayed and entered, handle the display of the date field in the TField's ONGETTEXT event, and the validity judgment of the input value in the ONSETTEXT event is determined. In order to reuse the code, the procedures and functions calling the ONGETTEXT and ONSETTEXT event processing 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 field display process, call in the OnGetText event}

Procedure DatefieldText (Sender: Tfield; Var Text: String);

{Date type field Enter the judgment function, call in the ONSETTEXT event}

Function DatefieldSettext (Sender: Tfield; Const text: String): Boolean;

IMPLEMentation

Procedure DatefieldText (Sender: Tfield; Var Text: String);

VAR

DDATE: TDATE;

Wyear, WMONTH, WDAY: WORD;

Arytestymd: array [1..2] of char; {Test Input Mask Temporary array}

IYMD: Integer;

Begin

DDATE: = sender.AssDateTime;

Decodedate (DDATE, WYEAR, WMONTH, WDAY);

{Test input mask included in the format.

Arytestymd: = 'year';

IF strscan (Pchar (Sender.editmask),

Arytestymd [1]) <> nil dam

IYMD: = 1;

Arytestymd: = 'Moon';

IF strscan (Pchar (Sender.editmask),

Arytestymd [1]) <> nil dam

IYMD: = 2;

Arytestymd: = 'Day'; if strscan (Pchar (Sender.editMask),

Arytestymd [1]) <> nil dam

IYMD: = 3;

Case IYMD OF

1: {Input Mask is: "YYYY Year" format.

TEXT: = INTOSTR (Wyear) 'Year';

2: {Enter Mask is: "YYYY Year MM" format.

TEXT: = INTOSTR (Wyear) 'Year' INTOSTR (WMONTH) 'Moon';

3: {Enter Mask is: "YYYY Year MM Month DD Day" format.

Text: = INTOSTR (WYEAR) ' INTOSTR (WMONTH) ' Moon ' INTOSTR (WDAY) ' Day '

Else {defaults to: "YYYY Year MM Month DD Day" format.

Text: = INTOSTR (WYEAR) ' INTOSTR (WMONTH) ' Moon ' INTOSTR (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

{Date with user input}

Syear: = COPY (Text, 1, 4);

SMONTH: = Copy (Text, 7, 2);

SDAY: = COPY (Text, 11, 2);

{Test input mask included in the format.

Arytestymd: = 'year';

IF strscan (Pchar (Sender.editmask),

Arytestymd [1]) <> nil dam

IYMD: = 1;

Arytestymd: = 'Moon';

IF strscan (Pchar (Sender.editmask),

Arytestymd [1]) <> nil dam

IYMD: = 2;

Arytestymd: = 'Day';

IF strscan (Pchar (Sender.editmask),

Arytestymd [1]) <> nil dam

IYMD: = 3;

{Using Try ... Except to input date conversion}

Try

Begin

Case IYMD OF

1: {Input Mask is: "YYYY Year" format.

Begin

DDATE: = STRTODATE (SYEAR '- 01-01'); {Chinese Windows default date format is: YYYY-mm-dd. Lower}

Sender.asdatetime: = DDATE;

END;

2: {Enter Mask is: "YYYY Year MM" format.

Begin

DDATE: = STRTODATE (SYEAR '-' SMONTH '- 01');

Sender.asdatetime: = DDATE;

END;

3: {Enter Mask is: "YYYY Year MM Month DD Day" format.

Begin

DDATE: = STRTODATE (SYEAR '-' SMONTH '-' SDAY);

Sender.asdatetime: = DDATE;

END;

Else {defaults to: "YYYY Year MM Month DD Day" format.

Begin

DDATE: = STRTODATE (SYEAR '-' SMONTH '-' SDAY);

Sender.asdatetime: = DDATE;

END;

END;

DatefieldSettext: = true;

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 unit}

Unit main;

Interface

Uses

... {去 Other Content}

Procedure Table1BIRTHDAYGETTEXT (Sender: Tfield; Var Text: String; DisplayText: Boolean);

Procedure Table1birdhdaySettext (Sender: Tfield; Const text: string);

Private

{Private Declarations}

public

{Public declarations}

……{slightly}

IMPLEMentation

{The customized unit contains coming.

Uses dbdateEditmaskTrans;

{$ R * .dfm}

...... {Other procedures}

Procedure TFORM1.MMACACTIVATE (Sender: TOBJECT);

{Set an input mask for a date field, you can put it in the TField field definition. }

Begin

Table1.fieldbyName ('birthday'). Editmask: = '9999 / year 99 / month 99 / day; 1; _';

END;

Procedure tForm1.table1birdhdaygettext (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

Abort; {Conversion is unsuccessful, date illegal}

END;

End.

转载请注明原文地址:https://www.9cbs.com/read-4129.html

New Post(0)