Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Share:
Notifications
Clear all

[Solved] Lazarus/Delphi - How to use TEdit to enter Integer or Float numbers

1 Posts
1 Users
0 Reactions
6,797 Views
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
Topic starter  

Quite often I need to limit the input capabilities of a TEdit so the user can only enter a number. Yes, I know TMaskEdit does that as well, I just don't like TMaskEdit.

For Integers:

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: char);
begin
  if (not (Key in ['0'..'9', #8, #9]))  then Key := #0;
end;    

For Real or floating numbers (mind the decimal point, this example uses "." for US usage) - this function also captures entering more than one decimal point:

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: char);
begin
  if (not (Key in ['0'..'9', '.', #8, #9])) OR ( (Key = '.') and (pos('.',TEdit(Sender).Text)>0) ) then Key := #0;
end;    

   
ReplyQuote
Share: