delphi里面控制exit只能输入数字和小数点

来源:百度知道 编辑:UC知道 时间:2024/05/14 19:35:37

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if not (key in ['0'..'9','.',#8,#32]) then
key:= #0;
end;

end.

但如果你想只输入数字而且有格式限制 那么你最好还是用第三方控件`

应该是Edit吧?可以在keypress里面加上如下代码,可以输入数字,并且可以使用退格删除数字,可以使用回车

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
case Key of
'0'..'9', #8, #13, #27, '.' : ;
else
begin
MessageBox(Handle, '请输入数字', PChar('输入错误'), MB_OK + MB_ICONINFORMATION);
Key := #0;
end;
end;
end;