给你个函数:
//EditType为限制的类型:1-任意输入;2-整数输入;3-浮点输入;
//Digits:小数位数
procedure FormatKeyPress(Text:string;SelStart,SelLength:integer;
var Key:Char;EditType:integer;Digits:integer);
begin
if (Key=#27) or (Key=#8) or (EditType=1) then exit;
if EditType=2 then
if not (Key in ['0'..'9','+','-'] ) then Key:=#0;
if EditType=3 then
if not (Key in ['0'..'9','+','-','.'] ) then Key:=#0;
//控制+-
if (Key ='-') or (Key='+' ) then
begin
if ((Pos('-',Text) > 0) or (Pos('+',Text) > 0 )) and
(SelLength=0 ) then Key:=#0;
if SelStart > 0 then Key:=#0;
end;
//控制.
if (Key = '.') and (EditType=3 ) then
begin
if (Pos('.',Text) > 0) and (not((SelStart=Pos('.',Text) ))) then Key:=#0;
if SelStart=0 then Key:=#0;
if (Digits>0) and (SelStart+SelLength=0) and (EditType=3) then
if (pos('.',Text )>0 ) and (SelStart>=pos('.',Text)) then
if length(Text)-pos('.',Text )>=Digits then Key:=#0;
end;
end;
procedure TNewStoveFM.Edt_QtyKeyPress(Sender: TObject; var Key: Char);
begin
FormatKeyPress(TEdit(Sender).Text,TEdit(Sender).SelStart,TEdit(Sender).SelLength,Key,3,4);
end;