Link to home
Start Free TrialLog in
Avatar of ionet
ionet

asked on

Accept only numbers

Hi, I was just wondering if there was an easy way to accept only numbers instead of a string in a TEdit or TMemo, etc? Thank you.
ASKER CERTIFIED SOLUTION
Avatar of Trekker72
Trekker72

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ionet
ionet

ASKER

Yes, see I already know that method I just have no idea what it means. Can someone please explain it?
from Delphi help...

TWinControl.OnKeyPress
Occurs when key pressed.
type TKeyPressEvent = procedure (Sender: TObject; var Key: Char) of object;
property OnKeyPress: TKeyPressEvent;

Description
Use the OnKeyPress event handler to make something happen as a result of a single character key press.
The Key parameter in the OnKeyPress event handler is of type Char; therefore, the OnKeyPress event registers the ASCII character of the key pressed.

...so basically when the edit box has the focus and a key is pressed the Char value is passed to the event.
The code i posted earlier checks to see if the key pressed was a numeral ('0'..'9') or backspace (#8) and if not set it to null (#0) so it does not get put into the edit box.  see www.asciitable.com for some ascii conversions
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial