formatar data ao digitar
Boa noite amigos,
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,
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
Curtidas 0
Melhor post
Flavio Maidl
19/06/2012
Tenho uma solução DE LONGE mais simples, a regra aplica-se a qualquer mascara que você quiser usar depois, basta alterar esse mesmo código:
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;
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;
GOSTEI 1
Mais Respostas
Ricardo_ascii
26/05/2008
Bom dia meu amigo,
O que você pode fazer é o seguinte: mascara ele no evento onexit do campo.
Ricardo
O que você pode fazer é o seguinte: mascara ele no evento onexit do campo.
Ricardo
GOSTEI 0
Martins
26/05/2008
Tente a função do [b:2d111bfcdc]Guaderio - membro do Active[/b:2d111bfcdc]
Usaria ela assim:
Acho q está função poderá lhe ser útil, retorne para informar.
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.
GOSTEI 0
Paulo
26/05/2008
Pelo que eu entendi da dúvida do jlsmendes não foi essa. Ele quer que ao digitar os dois primeiros digitos de uma data, o sistema já coloca a primeira barra e quando ele digitar o mes, coloca a outra barra e ele digita o ano.
GOSTEI 0
Jlsmendes
26/05/2008
Martins, simplesmente perfeito.
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.
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.
GOSTEI 0
Martins
26/05/2008
Pelo que eu entendi da dúvida do jlsmendes não foi essa. Ele quer que ao digitar os dois primeiros digitos de uma data, o sistema já coloca a primeira barra e quando ele digitar o mes, coloca a outra barra e ele digita o ano.
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.
GOSTEI 0
Paulo
26/05/2008
Havia feito um teste da função e não funcionou, pois o fiz de forma errada e postei achando que não havia entendido a dúvida do colega jlsmendes, mas fiz outro teste e funcionou legal. Desculpam-me pelo post ´apressado´, mas funciona sim a função.
GOSTEI 0
Martins
26/05/2008
Havia feito um teste da função e não funcionou, pois o fiz de forma errada e postei achando que não havia entendido a dúvida do colega jlsmendes, mas fiz outro teste e funcionou legal. Desculpam-me pelo post ´apressado´, mas funciona sim a função.
OK colega, sem problema, estamos aqui para ajudar e aprender com os demais colegas.
Boa sorte a todos!!!
GOSTEI 0
Leufmt
26/05/2008
Esta dica é file. Agora vou ajustar para CPF e CNPJ.
Obrigado Martins
Obrigado Martins
GOSTEI 0
Leufmt
26/05/2008
Ai vai o código altera para CPF e CNPJ para quem quiser.
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;
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;
GOSTEI 0
Flavio Maidl
26/05/2008
Tenho uma solução DE LONGE mais simples, a regra aplica-se a qualquer mascara que você quiser usar depois, basta alterar esse mesmo código:
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;
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
GOSTEI 1
Flavio Maidl
26/05/2008
Tenho uma solução DE LONGE mais simples, a regra aplica-se a qualquer mascara que você quiser usar depois, basta alterar esse mesmo código:
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;
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
GOSTEI 0