Ver Mensaje Individual
  #21  
Antiguo 10-05-2017
Avatar de Soa Pelaez
Soa Pelaez Soa Pelaez is offline
Miembro
 
Registrado: nov 2015
Posts: 133
Reputación: 9
Soa Pelaez Va por buen camino
Cita:
Empezado por olbeup Ver Mensaje
Hola Soa Pelaez,

Sólo quieres controlar si al pulsar un (.) se cambie por una (,) y delimitar el número de decimales que puedes introducir, pues yo utilizo esto.

Código Delphi [-]
procedure ValidOnKeyPress(var Key: Char; MaxDecimals: Integer; Text: String);

  const
    WVK_POINT       = '.';
    WVK_COMMA       = ',';
    WVK_NULL        = #0;
    WVK_BACKDELETE  = #8;


  function CountDecimals: Byte;
  var
    nPos: Integer;
  begin
    Result := 0;
    nPos := Pos(',', Text);
    if (nPos <> 0) or (nPos <> Length(Text)) then
      Result := (Length(Text) - nPos);
  end;

begin
  if (Key = WVK_POINT) then
    Key := WVK_COMMA;

  if not (Key in ['0'..'9', WVK_BACKDELETE, WVK_COMMA]) then
    Key := WVK_NULL;

  if (Key = WVK_COMMA) and (Pos(WVK_COMMA, Text) > 0) then
    Key := WVK_NULL;

  if (CountDecimals = MaxDecimals) and (not (Key = WVK_BACKDELETE)) then
    Key := WVK_NULL;
end;
Y la llamada lo hacemos asi.
Código Delphi [-]
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  ValidOnKeyPress(Key, 2, Edit1.Text);
end;
O
Código Delphi [-]
procedure TForm1.DBEdit1KeyPress(Sender: TObject; var Key: Char);
begin
  ValidOnKeyPress(Key, 2, DBEdit1.Text);
end;
Un saludo.
Primero muchas gracias por tu aporte.
Pero sigue con la misma falla que tiene mi código porque lo ensaye y al compilarlo e ingresar un punto en el edit no me lo modifica por la coma y a su vez con tu código si escribo muchas comas lo deja hacer.
Responder Con Cita