Processing the display and input of database date fields in Delphi

zhaozj2021-02-17  43

---- When using Delphi for database design, inevitable will 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 Thought

---- Use the TField's EditMask attribute to simultaneously use the display and input mask, and 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; InterfaceUses Windows, Sysutils, Controls, Forms, DB;

{Date type field display process, call} Procedure DatefieldGetText (Sender: Tfield; Var Text: string) in the ONGETTEXT event;

{Date type field Enter the judgment function, call} function datefieldsettext (sender: tfield; const text: boolean): boolean;

IMPLEMENTATION

Procedure DatefieldText (Sender: Tfield; Var Text: TDATE; WYEAR, WMONTH, WDAY: WORD; Arytestymd: array [1..2] of char; {test input mask Temporary array} IYMD: Integer Begin Ddate: = sender.asdatetime; decodedate (DDATE, WYEAR, WMONTH, WDAY);

{Test input mask included in the format.} Arytestymd: = 'year'; if strscan (Pchar (Sender.editmask), Arytestymd [1]) <> nil damd: = 1; Arytestymd: = 'Month; IF STRSCAN (Pchar (Sender.editMask), Arytestymd [1]) <> nil damd: = 2; Arytestymd: = 'Japan; if strscan (Pchar (Sender.editmask), Arytestymd [1]) <> nil damd : = 3; Case IYMD OF 1: {Input Mask is: "YYYY Years" format.} Text: = INTOSTR (Wyear) 'Year'; 2: {Input Mask is: "YYYY Year MM" Format.} Text: = INTOSTR (Wyear) 'Year' INTOSTOSTR (WMONTH) 'Moon'; 3: {Enter Mask is: "YYYY Year MM Month DD Day" format.} Text: = INTOSTR (Wyear ) 'Year' INTOSTR (WMONTH) 'Moon' INTOSTR (WDAY) 'Day'; Else {defaults to: "YYYY Year MM Month DD Day" format.} Text: = INTOSTR (Wyear) ' Year ' INTOSTR (WMONTH) ' Moon ' INTOSTR (WDAY) ' Day '; end;

END;

Function DatefieldSettext (sext: string): boolean; var ddate: Tdate; Syear, SMONTH, SDAY: STRING; Arytestymd: Array [1..2] of char; ximd: integer; begin {get user input Date} 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 damd: = 1; Arytestymd: = 'Month; IF STRSCAN (Pchar (Sender.editMask), Arytestymd [1]) <> nil damd: = 2; Arytestymd: = 'Japan; if strscan (Pchar (Sender.editmask), Arytestymd [1]) <> nil damd : = 3;

{Using Try ... Except to input a date conversion} try begin case imd of 1: {Enter Mask is: "YYYY Year" format.} Begin Ddate: = STRTODATE (SYEAR '- 01-01'); {Chinese Windows The default date format is: yyyy-mm-dd. 同} sender.asdatetime: = DDATE; END; 2: {Enter Mask is: "YYYY Year M Moon" format.} Begin DDATE: = Strtodate (Syear ' - ' SMONTH ' - 01 '); sender.asdatetime: = DDATE; END; 3: {Input Mask is: "YYYY Years MM Month DD Day" format.} Begin Ddate: = STRTODATE (SYEAR ' - ' SMONTH '-' SDAY; sender.asdatetime: = DDATE; END; Else {defaults: "YYYY Years MM Month DD Day" format. 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.

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

New Post(0)