For Newbie

Tuesday, 15 November 2011

Number only input in TEdit on Delphi

Preventing the input from the user on the TEdit than numbers. Useful for the lawyer-input numbers (age, price .. et al)

When the user input data on the TEdit, then the event that work is onKeyPress. Suppose the control input is Edit1, edit the event onKeyPress
TForm1.Edit1KeyPress procedure (Sender: TObject; var Key: Char);
   {Figures starting from 0-9, while the ASCII Key Code for 0-9
   is 48 s / d 57 ... If the keycode> 47 'and' <58 then
   input is allowed. Since the backspace still required to
   delete, then the keycode for the Backspace (8) is allowed, so
   plus 'or' = 8}
   if (Key> Chr (47)) and (Key <Chr (58)) or (Key = Chr (8)) then {...} No Action
   / / Updating the anti-cattle inputs in addition to the numbers ...
   Key: = Chr (0);

You can also apply these tricks on a TMemo component.

You should know ...
Users are still able to input data via the clipboard alias other than the numbers in the paste (CTRL + V). But do not despair, there are still ways to outsmart the order given input is a number. It can be seen on the page: Prevent Paste From Clipboard in the Component Editor. That way, your efforts will not be in vain. But its weakness is the user can not paste from clipboard.

Share on Facebook
Share on Twitter
Share on Google+

Related : Number only input in TEdit on Delphi