VCL Delphi

Title: How to validate numeric input in a TEdit
uses
ClipBrd;
procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
Mgs: TMsg;
procedure EatKey;
{ Clear Keyboardbuffer / Tastaturbuffer leeren }
begin
PeekMessage(Mgs, 0, WM_CHAR, WM_CHAR, PM_REMOVE);
end;
begin
{ Check if control pressed / ¨¹berpr¨¹fen, ob Control Taste gedr¨¹ckt }
if (ssCtrl in Shift) then
case key of
{ Ctrl + v }
86:
begin
{ determine if the Clipboard contains a string type }
{ ¨¹berpr¨¹fen, ob es Text in der Zwischenablage hat }
if Clipboard.HasFormat(CF_TEXT) then
{ Check if text consists of numbers }
{ ¨¹berpr¨¹fen, ob der Text aus Zahlen besteht }
try
StrToInt(Clipboard.AsText);
except
{ If no then don't insert text }
{ Wenn nein, dann ignoriere das Einf¨¹gen }
EatKey
end;
end;
{ Ctrl + c }
67: { do nothing / nichts machen }
else
EatKey;
end
{ else check for allowed characters such as BackSpace, RETURN...}
{ sonst auf erlaubte Zeichen ¨¹berpr¨¹fen }
else if not (Char(Key) in [#8, #13, #46, #48..#57,#96..#105]) then EatKey;
end;
// Use your own Popup Menu to customize paste.