Fórum so aceitar numero no edit #397026
10/03/2011
0
se for salvar letra mostre um chowmessage ('so aceita numero')
Alessandra
Curtir tópico
+ 0Post mais votado
10/03/2011
function Ret_Numero(Key: Char; Texto: string; EhDecimal: Boolean = False): Char;
begin
if not EhDecimal then
begin
{ Chr(8) = Back Space }
if not ( Key in ['0'..'9', Chr(8)] ) then
Key := #0
end
else
begin
if Key = #46 then
Key := DecimalSeparator
;
if not ( Key in ['0'..'9', Chr(8), DecimalSeparator] ) then
Key := #0
else
if ( Key = DecimalSeparator ) and ( Pos( Key, Texto ) > 0 ) then
Key := #0
;
;
end
;
Result := Key;
end;
Onde e como chamar:
{ Evento OnKeyPress do seu Edit }
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
begin
Key := Ret_Numero( Key, Edit1.Text );
end;
Espero ter colaborado.
Wilson Junior
Gostei + 1
Mais Posts
10/03/2011
Marco Salles
Gostei + 0
10/03/2011
Alessandra
quando clicase no botão salvar ela aparecesse a mensagem ('aceita apenas numero')
se eu for colocar com o OnKeyPress ele altomaticamen aparece a mensagem
Gostei + 0
10/03/2011
Wilson Junior
{ Evento OnKeyPress do seu Edit }
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
begin
Key := Ret_Numero( Key, Edit1.Text ); if Key = #0 then ShowMessage('Aceita apenas números') ;
end;
Espero ter colaborado.
Gostei + 0
10/03/2011
Alessandra
Gostei + 0
10/03/2011
Marco Salles
procedure TForm1.Button2Click(Sender: TObject); var s:String; i:integer; begin s:=''; for I := 1 to length(edit1.Text) do if CharInSet(edit1.Text[i],['0'..'9']) then s:=s+edit1.Text[i]; showmessage(s); end;
Gostei + 0
30/05/2013
José
Gostei + 0
03/06/2013
Guilherme Wiethaus
...ou pode-se utilizar também componentes gratuitos e muito bom que vai te atender neste quesito. O JCL/JVCL (Project Jedi Components). Utilize o JvValidateEdit que possui o que procura com outras características e funcionalidades interessantes.
Abraços
Gostei + 0
07/06/2013
Rafael Cruz
if not (key in [0..9,#8, #13]) then key := #0;
Gostei + 0
08/06/2013
Marco Salles
if not (key in [0..9,#8, #13]) then key := #0;
Veja ela não quer utilizar o evento
quando clicase no botão salvar ela aparecesse a mensagem (aceita apenas numero)
se eu for colocar com o OnKeyPress ele altomaticamen aparece a mensagem
entende ???
Gostei + 0
08/06/2013
Rafael Cruz
se for tenho uma função que pode ajudar eu acho
Function SoNumero(STR : String) : String;
// Filtra todos os numeros de uma string
var
Ind : Integer;
TmpRet : String;
begin
TmpRet := ;
for Ind := 1 to Length(STR) do
begin
if (Copy(STR,Ind,1) = 1) or (Copy(STR,Ind,1) = 2) or (Copy(STR,Ind,1) = 3) or (Copy(STR,Ind,1) = 4) or
(Copy(STR,Ind,1) = 5) or (Copy(STR,Ind,1) = 6) or (Copy(STR,Ind,1) = 7) or
(Copy(STR,Ind,1) = 8) or (Copy(STR,Ind,1) = 9) or (Copy(STR,Ind,1) = 0) then
begin
TmpRet := TmpRet + Copy(STR, Ind, 1);
end;
end;
Result := TmpRet;
end;
no click do botao pode colocar
if (length(edit1.text)) (length(sonumero(edit1.text))) then mensagem
Mais ai me pergunto.. se é um campo que só aceita gravar numero, pq fazer deixar o usuário digitar letra clicar no salvar e ter que voltar no edit corrigir pra salvar de novo.
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)