formatar data ao digitar
26/05/2008
0
Gostaria de fazer uma coisa em meu sistema que sempre vejo em vários outros sistemas, que é a colocação da / automaticamente ao digitar datas.
Por exemplo:
tenho a data 26/05/2008
Quando digito o 26, automaticamente o sistema insere uma barra /, depois digito o 05 e novamente o sistema insere outra barra.
Isso é possível no Delphi 7?
Forte Abraço,
Jlsmendes
Post mais votado
19/06/2012
usei no teste um componente chamado edit2, mas pode ser usado em dbedit também...
detalhe importante: no exemplo estou formatando para digitar por exemplo 07041974 e o edit se
formatar sozinho conforme for digitando tornado o resultado final 07/04/1974. como estou usando
uma data que nunca ira ultrapassar 10 caracteres, então é importante colocar na propriedade maxlength
do componente o valor 10.
coloque esse código no evento onchange do edit
if Length(edit2.Text) = 2 then begin
edit2.Text:= edit2.Text+ /;
Edit2.SelStart := Length( Edit2.Text );
end;
if Length(edit2.Text) = 5 then begin
edit2.Text:= edit2.Text+ /;
Edit2.SelStart := Length( Edit2.Text );
end;
Flavio Maidl
Mais Posts
26/05/2008
Ricardo_ascii
O que você pode fazer é o seguinte: mascara ele no evento onexit do campo.
Ricardo
26/05/2008
Martins
function valida_mascara_data(var Key: char; Sender: TObject):string; begin if not(key in[´0´..´9´,8,13]) then key := 0; if key <> 8 then begin if key <> 13 then if Length(TCustomEdit(sender).Text) = 10 then TCustomEdit(sender).Clear; case Length(TCustomEdit(sender).Text) of 2:begin if not (key in [´0´..´9´]) then key := 0; TCustomEdit(sender).Text := TCustomEdit(sender).Text + ´/´; TCustomEdit(sender).selstart := Length(TCustomEdit(sender).text); end; 5:begin if not (key in [´0´..´9´]) then key := 0; TCustomEdit(sender).Text := TCustomEdit(sender).Text + ´/´; TCustomEdit(sender).selstart := Length(TCustomEdit(sender).text); end; 10:begin if key <> #13 then begin TCustomEdit(sender).Clear; end; end; end; end; end;
Usaria ela assim:
//No OnKeyPress do Componente. valida_macara_data(key,sender);
Acho q está função poderá lhe ser útil, retorne para informar.
26/05/2008
Paulo
26/05/2008
Jlsmendes
Testado, aprovado.
Né por nada não, mas muita gente procura por esse código, e na boa não tinha em nenhum site da internet, pois já estava ´googlando´ a mais de 2 semanas.
Mais uma vez muito obrigado.
26/05/2008
Martins
Paulo não testei a função acima, mas creio que ela faz o q o colega deseja.
Mas não testei, vou testar logo mais, mas caso não funcione, escreverei uma para ele.
26/05/2008
Paulo
26/05/2008
Martins
OK colega, sem problema, estamos aqui para ajudar e aprender com os demais colegas.
Boa sorte a todos!!!
28/04/2009
Leufmt
Obrigado Martins
28/04/2009
Leufmt
function Mascara_CPF(var Key: char; Sender: TObject): String;
begin
If not(Key in[´0´..´9´, #8, 13]) then
Key := 0;
If Key <> 8 then
begin
If Key <> 13 then
begin
If Length(TCustomEdit(Sender).Text) = 14 then
TCustomEdit(Sender).Clear;
end;
Case Length(TCustomEdit(Sender).Text) of
3: begin
If not (Key in [´0´..´9´]) then
Key := 0;
TCustomEdit(Sender).Text := TCustomEdit(Sender).Text + ´.´;
TCustomEdit(Sender).SelStart := Length(TCustomEdit(Sender).Text);
end;
7: begin
If not (Key in [´0´..´9´]) then
Key := 0;
TCustomEdit(Sender).Text := TCustomEdit(Sender).Text + ´.´;
TCustomEdit(Sender).SelStart := Length(TCustomEdit(Sender).Text);
end;
11: begin
If not (Key in [´0´..´9´]) then
Key := #0;
TCustomEdit(Sender).Text := TCustomEdit(Sender).Text + ´-´;
TCustomEdit(Sender).SelStart := Length(TCustomEdit(Sender).Text);
end;
14: begin
If Key <> 13 then
TCustomEdit(Sender).Clear;
end;
end;
end;
end;
function Mascara_CNPJ(var Key: char; Sender: TObject): String;
begin
If not(Key in[´0´..´9´, 8, 13]) then
Key := 0;
If Key <> 8 then
begin
If Key <> 13 then
begin
If Length(TCustomEdit(Sender).Text) = 18 then
TCustomEdit(Sender).Clear;
end;
Case Length(TCustomEdit(Sender).Text) of
2: begin
If not (Key in [´0´..´9´]) then
Key := 0;
TCustomEdit(Sender).Text := TCustomEdit(Sender).Text + ´.´;
TCustomEdit(Sender).SelStart := Length(TCustomEdit(Sender).Text);
end;
6: begin
If not (Key in [´0´..´9´]) then
Key := #0;
TCustomEdit(Sender).Text := TCustomEdit(Sender).Text + ´.´;
TCustomEdit(Sender).SelStart := Length(TCustomEdit(Sender).Text);
end;
10: begin
If not (Key in [´0´..´9´]) then
Key := 0;
TCustomEdit(Sender).Text := TCustomEdit(Sender).Text + ´/´;
TCustomEdit(Sender).SelStart := Length(TCustomEdit(Sender).Text);
end;
15: begin
If not (Key in [´0´..´9´]) then
Key := 0;
TCustomEdit(Sender).Text := TCustomEdit(Sender).Text + ´-´;
TCustomEdit(Sender).SelStart := Length(TCustomEdit(Sender).Text);
end;
18: begin
If Key <> 13 then
TCustomEdit(Sender).Clear;
end;
end;
end;
end;
19/06/2012
Flavio Maidl
usei no teste um componente chamado edit2, mas pode ser usado em dbedit também...
detalhe importante: no exemplo estou formatando para digitar por exemplo 07041974 e o edit se
formatar sozinho conforme for digitando tornado o resultado final 07/04/1974. como estou usando
uma data que nunca ira ultrapassar 10 caracteres, então é importante colocar na propriedade maxlength
do componente o valor 10.
coloque esse código no evento onchange do edit
if Length(edit2.Text) = 2 then begin
edit2.Text:= edit2.Text+ /;
Edit2.SelStart := Length( Edit2.Text );
end;
if Length(edit2.Text) = 5 then begin
edit2.Text:= edit2.Text+ /;
Edit2.SelStart := Length( Edit2.Text );
end;
DETALHE : nao esqueca de colocar / senao da erro, no exemplo coloquei sem as
19/06/2012
Flavio Maidl
usei no teste um componente chamado edit2, mas pode ser usado em dbedit também...
detalhe importante: no exemplo estou formatando para digitar por exemplo 07041974 e o edit se
formatar sozinho conforme for digitando tornado o resultado final 07/04/1974. como estou usando
uma data que nunca ira ultrapassar 10 caracteres, então é importante colocar na propriedade maxlength
do componente o valor 10.
coloque esse código no evento onchange do edit
if Length(edit2.Text) = 2 then begin
edit2.Text:= edit2.Text+ /;
Edit2.SelStart := Length( Edit2.Text );
end;
if Length(edit2.Text) = 5 then begin
edit2.Text:= edit2.Text+ /;
Edit2.SelStart := Length( Edit2.Text );
end;
DETALHE : nao esqueca de colocar / senao da erro, no exemplo coloquei sem as
Clique aqui para fazer login e interagir na Comunidade :)