domingo, 13 de dezembro de 2009

Facilitating input of date

In any document, generally we have that to inform dates, in way that if becomes necessary to speed the input of keyboard. Then, i created two routines, that already use have 6 years. If you are experienced, is something easy, but for the beginning ones, this tip can be interesting.

How it functions the routines?

They are two routines:
Validakeydatas and Validakeydatas2.
Using keyboard key RETURN functions validating the date when.
In mine in case that it has been very useful, therefore these routines beyond validating the dates have other advantages: .

.It speeds in the digitação, therefore when typing the date does not need to place the separator, in the case the bar ' /' , and additionally, it does not need to inform the year of the date, that the complete routine automatically
.It can be used in any component, as Edit, DBEdit, DBGrid.


These routines intercept the keyboard input of the user, returning to caracter null in the case to type something invalid date.

The difference of Validakeydatas and Validakeydatas2 is that in the second function the year for automatic fulfilling can be informed.

For example, the date '01/12/2009' you type only ' 01122009' or ' 0112' and uses a keyboard RETURN.

How to use the routines?

It is enough to configure the OnKeyPress event of the component, the following form:
Form creates one
It inserts two TEdit components in form
Of a double click in the OnKeyPress event of the Edit1

Example 1
Validakeydatas



procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: char);
begin
//Validakeydatas flame, not accepted blank date
Validakeydatas(Sender,Key,False);
if key=#13 then Edit2.setfocus;
if key=#27 then close;

end;

Example 2
Validakeydatas


procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: char);
begin
//alidakeydatas flame, but accepted blank date
Validakeydatas(Sender,Key,True);
if key=#13 then Edit2.setfocus;
if key=#27 then close;

end;


Example 3
Using Validakeydatas2


procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: char);
begin
//Call Validakeydatas
//don't accept blank date
//if not to inform the year, fills with ' 2009'
Validakeydatas2(Sender,Key,False,'2009');
if key=#13 then Edit2.setfocus;
if key=#27 then close;

end;


Source code

It follows below an example of Unit that could be used to store the functions:


unit funcoes1;

interface

uses

LCLIntf, Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
EditBtn, ExtCtrls, StdCtrls;

Function IsDate(wData:String):Boolean;
Procedure ValidaKeyDatas(Const Sender:TObject; var key: char; Const AceitaNulo: Boolean);
Procedure ValidaKeyDatas2(Const Sender:TObject; var key: char; Const AceitaNulo: Boolean;Const zAno:String);

implementation

Function IsDate(wData:String):Boolean;
var
T:TDateTime;
Begin
Try
T:=StrToDateTime(wData);
Result:=True;
except
Result:=False;
end;
end;


Procedure ValidaKeyDatas(Const Sender:TObject; var key: char; Const AceitaNulo: Boolean);
var
D1:String;
L:integer;
begin
if not (key in ['0'..'9','/',#8,#13,#27]) Then key:=#0;
if Sender.ClassName='TEdit' Then
begin
D1:=TEdit(Sender).Text;
if (key=#13) and (D1='') then
if not (AceitaNulo) Then key:=#0;

if (key=#13) and (D1<>'') then
begin
L:=length(D1);
if (pos('/',TEdit(Sender).Text)=0) and ((L=6) or (L=8)) then
begin
if L=6 then
//original version brazilian
//D1:=copy(D1,1,2)+'/'+copy(D1,3,2)+'/'+copy(D1,5,2)
//English version
D1:=copy(D1,3,2)+'/'+copy(D1,1,2)+'/'+copy(D1,5,2)
else
//original version brazilian
//D1:=copy(D1,1,2)+'/'+copy(D1,3,2)+'/'+copy(D1,5,4);
//English version
D1:=copy(D1,3,2)+'/'+copy(D1,1,2)+'/'+copy(D1,5,4);
end;
if isdate(D1) Then TEdit(Sender).Text:=D1;
if not isdate(D1) Then
begin
key:=#0;
//original version brazilian
//ShowMessage('Data Invalida');
//English version
ShowMessage('Invalid Date');
TEdit(Sender).SetFocus;
end;
end;
end;

end;

{
AceitaNulo: Accept Blank Date
zAno: Year to substitute
}

Procedure ValidaKeyDatas2(Const Sender:TObject; var key: char; Const AceitaNulo: Boolean;Const zAno:String);
var
D1:String;
L:integer;
begin
if not (key in ['0'..'9','/',#8,#13,#27]) Then key:=#0;
if Sender.ClassName='TEdit' Then
begin
D1:=TEdit(Sender).Text;
if (key=#13) and (D1='') then
if not (AceitaNulo) Then key:=#0;

if (key=#13) and (D1<>'') then
begin
L:=length(D1);
if (pos('/',TEdit(Sender).Text)=0) and ((L=6) or (L=8)) then
begin
if L=6 then
//brazilian version
//D1:=copy(D1,1,2)+'/'+copy(D1,3,2)+'/'+copy(D1,5,2)
D1:=copy(D1,3,2)+'/'+copy(D1,1,2)+'/'+copy(D1,5,2)
else
//brazilian version
//D1:=copy(D1,1,2)+'/'+copy(D1,3,2)+'/'+copy(D1,5,4);
D1:=copy(D1,3,2)+'/'+copy(D1,1,2)+'/'+copy(D1,5,4);
end;

if (pos('/',TEdit(Sender).Text)=0) and (L=4) then
begin
D1:=D1+zAno;
L:=length(D1);
if L=6 then
//D1:=copy(D1,1,2)+'/'+copy(D1,3,2)+'/'+copy(D1,5,2)
D1:=copy(D1,3,2)+'/'+copy(D1,1,2)+'/'+copy(D1,5,2)
else
//D1:=copy(D1,1,2)+'/'+copy(D1,3,2)+'/'+copy(D1,5,4);
D1:=copy(D1,3,2)+'/'+copy(D1,1,2)+'/'+copy(D1,5,4);
end;
if isdate(D1) Then TEdit(Sender).Text:=D1;
if not isdate(D1) Then
begin
key:=#0;
//ShowMessage('Data Invalida');
ShowMessage('Invalid Date');
TEdit(Sender).SetFocus;
end;
end;
end;
end;


end.


Until the next one.

Nenhum comentário:

Postar um comentário