如何添加只接受数字的 TEdit
?我搜索了很多资料,但没有找到有帮助的信息。
我需要一个只能输入数字而不能输入字母或字符串的 TEdit
。
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if not (Key in [#8, '0'..'9', DecimalSeparator]) then
begin
ShowMessage('Invalid key: ' + Key);
Key := #0;
end
else
if (Key = DecimalSeparator) and (Pos(Key, Edit1.Text) > 0) then
begin
ShowMessage('Invalid Key: twice ' + Key);
Key := #0;
end;
end;