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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER