Código
// Objeto -> Edit1 ( Objeto Tedit )
// Texto -> Edit1.Text
// VKey -> Key,, tecla precionada
// Espaco -> tamanho final da string
// Decimal -> Casas decimais da string
// Fonte: Planeta Delphi
// Modificada por: João Carlos Bassi Prado.
// Esta função serve para Formatar e alinhar a direita entradas numéricas em campos Tedit.
// Foi testada e funciona muito bem
function TForm2.fnumD(Objeto: TEdit; Texto, VKey: String; Espaco, Decimal: Integer): String;
Var
vChar, vDiv : String;
I : Integer;
begin
vDiv := '1';
For I := 1 to Decimal do
vDiv := vDiv + '0';
vChar := IfThen( VKey = #8, copy(Texto,1,length(Texto)-1),Texto+VKey );
While ( pos( ',', vChar ) > 0 ) or ( pos( '.', vChar ) > 0 ) do
begin
Delete( vChar, pos( '.', vChar ), 1 );
Delete( vChar, pos( ',', vChar ), 1 );
end;
Objeto.MaxLength := Espaco-1;
Objeto.Text := Format('%*.*n',[Espaco,Decimal,StrToFloat(vChar)/StrToInt(vDiv)]);
Objeto.SelStart := length(Objeto.text)+1;
end;
Exemplo:
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if not (key in ['0'..'9', #8]) then
key := #0;
Form2.fnumD(Edit1,Edit1.Text,key,50,2);
end;
Planeta Delphi - www.planetadelphi.com.br - Todos os direitos reservados | Copyright 2001-2009