Delphi中Edit输入控制必需为数字(2位小数)
procedure TfrmGoods_InfoAdd.edt_PriceKeyPress(Sender: TObject; var Key: Char);
var
DotPos,Len:integer;
begin
DotPos:=pos('.',(Sender as TEdit).Text);
Len:=length((Sender as TEdit).Text);
if key in ['0'..'9',#8,'.','+','-'] then
begin
if (key in ['+','-']) then
begin
//只能在光标处于第一的位置,才能输入'+'或者'-'
if ((Sender as TEdit).SelStart>0) then
begin
key:=#0;
exit;
end;
//'+','-'互斥
if (pos('+',(Sender as TEdit).Text)>0) or (pos('-',(Sender as TEdit).Text)>0) then
begin
key:=#0;
exit;
end;
end;
if key in ['.'] then
begin
//只能输入一个'.'
if (DotPos>0) then
begin
key:=#0;
exit;
end;
//只能在'+','-'后面输入'.'
if (pos('+',(Sender as TEdit).Text)>0) or (pos('-',(Sender as TEdit).Text)>0) then
if ((Sender as TEdit).SelStart<=0) then
begin
key:=#0;
exit;
end;
//小数点必须在最后两位
if (DotPos<=0) and ((Sender as TEdit).SelStart<Len-2) then
begin
key:=#0;
exit;
end;
end;
if key in ['0'..'9'] then
//小数点后面最多两位
if (DotPos>0) and (DotPos<=Len-2) and ((Sender as TEdit).SelStart>=DotPos) then
key:=#0 ;
end
else
key:=#0;
end;
var
DotPos,Len:integer;
begin
DotPos:=pos('.',(Sender as TEdit).Text);
Len:=length((Sender as TEdit).Text);
if key in ['0'..'9',#8,'.','+','-'] then
begin
if (key in ['+','-']) then
begin
//只能在光标处于第一的位置,才能输入'+'或者'-'
if ((Sender as TEdit).SelStart>0) then
begin
key:=#0;
exit;
end;
//'+','-'互斥
if (pos('+',(Sender as TEdit).Text)>0) or (pos('-',(Sender as TEdit).Text)>0) then
begin
key:=#0;
exit;
end;
end;
if key in ['.'] then
begin
//只能输入一个'.'
if (DotPos>0) then
begin
key:=#0;
exit;
end;
//只能在'+','-'后面输入'.'
if (pos('+',(Sender as TEdit).Text)>0) or (pos('-',(Sender as TEdit).Text)>0) then
if ((Sender as TEdit).SelStart<=0) then
begin
key:=#0;
exit;
end;
//小数点必须在最后两位
if (DotPos<=0) and ((Sender as TEdit).SelStart<Len-2) then
begin
key:=#0;
exit;
end;
end;
if key in ['0'..'9'] then
//小数点后面最多两位
if (DotPos>0) and (DotPos<=Len-2) and ((Sender as TEdit).SelStart>=DotPos) then
key:=#0 ;
end
else
key:=#0;
end;