Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 1 of 1 total
Thread Using TEdit for number entry
Thu, Sep 23 2021 8:31 AMPermanent Link

Paul Coshott

Avatar

Hi All,

Using the OnKeyDown event, I have written some code to only allow number entry for a TEdit control, plus left right arrows, home, end, backspace, delete keys etc.

I have 2 problems. First I’d like to limit the whole number part to a certain length of characters, and also the decimal part, so I can limit it to 2 decimals for example.

And secondly, if I’m at the maximum, but the edit text is selected, I want it to overwrite what’s selected. But currently, it doesn’t.

Any chance anyone could improve this code or suggest a better way to do it.

function TfOptionsTools.eMCurrentKeyDown(Sender: TObject; Key: Integer; ShiftKey: Boolean; CtrlKey: Boolean; AltKey: Boolean): Boolean;
begin
 if ((Key >= 48) and (Key <= 57)) or ((Key >= 96) and (Key <= 105)) then begin
   if Pos('.', TEdit(Sender).Text) = 0 then begin
     Result := Length(TEdit(Sender).Text) <= 1;
   end else begin
     Result := Length(TEdit(Sender).Text) <= 3;
   end;
 end else if (Key = 110) then begin
   Result := Pos('.', TEdit(Sender).Text) = 0;
 end else if (Key = 8) or (Key = 9) or (Key = 37) or (Key = 39) or (Key = 46) then begin
   Result := True;
 end else begin
   Result := False;
 end;
end;

Thanks,
Paul
Image