找回密码
 立即注册
查看: 457|回复: 0

DELPHI EDIT控件 限制成只能输入数字

[复制链接]

194

主题

0

回帖

986

积分

管理员

积分
986
发表于 2024-6-15 17:28:09 | 显示全部楼层 |阅读模式
Edit中只能输入数字且只能输入一个小数点
procedure Tform1.Edit1KeyPress(Sender: TObject; var Key: Char);
var
edt: TEdit;
str, strL, strR: string;
p: integer;
begin
   // 获取当前文本内容, 注意要去掉选中部分(因为会被改写).
edt := TEdit(Sender);
str := edt.text;
if Length(edt.SelText) <> 0 then
begin
   strL := LeftStr(edt.text, edt.SelStart);
   strR := RightStr(edt.text, Length(edt.text) - edt.SelStart - edt.SelLength);
   str := strL + strR;
end;
   // 限制输入数字/小数点/退格键
if not (Key in [#8, #13, #127, '.', '-', '0'..'9']) then Key := #0;
   //限制只能输入一个小数点
if Key = '.' then
begin
   p := Pos('.', edt.Text);
   if p > 0 then Key := #0;
end;
   //限制只能在第一位输入且只能输入一个'-'号
if Key = '-' then
begin
   if edt.SelStart > 0 then Key := #0;
   p := Pos('-', edt.Text);
   if p > 0 then Key := #0;
end;
end;
商务合作
业务洽谈
Wechat:S3-SH-CN  
QQ群:128669090
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|S3软件

GMT+8, 2026-6-6 07:57 , Processed in 0.036489 second(s), 19 queries .

Powered by S3

©2001-2025 S3 Team.

快速回复 返回顶部 返回列表