我修改了别人的一个函数, 用Tag区分 Tag=0 随意; Tag=1 整形; Tag=2 浮点;
你也可以重写onkeypress事件,集成到tedit中
procedure FormatKeyPress(Sender:TObject;var Key:Char;Digits:integer);
var
myEdit:tsuiEdit;
begin
if (Sender is TSuiEdit) then
begin
myEdit:= Sender as Tsuiedit;
if (Key=#27) or (Key=#8) or (myEdit.Tag=0) then exit;
if myEdit.Tag=1 then
if not (Key in ['0'..'9','+','-'] ) then Key:=#0;
if myEdit.Tag=2 then
if not (Key in ['0'..'9','+','-','.'] ) then Key:=#0;
//控制+-
if (Key ='-') or (Key='+' ) then
begin
if ((Pos('-',myEdit.Text) > 0) or (Pos('+',myEdit.Text) > 0 )) and
(myEdit.SelLength=0 ) then Key:=#0;
if myEdit.SelStart > 0 then Key:=#0;
end;
//控制.
if (Key = '.') and (myEdit.Tag=2 ) then
begin
if (Pos('.',myEdit.Text)>0) then
begin
if (myEdit.SelStart+myEdit.SelLength<Pos('.',myEdit.Text))
or (myEdit.SelStart>=Pos('.',myEdit.Text)) then Key:=#0;
if myEdit.SelLength=0 then Key:=#0;
end;
if myEdit.SelStart=0 then Key:=#0;
if (Digits>0) and (myEdit.SelStart>0) and (myEdit.Tag=2 ) then
if length(myEdit.Text)-myEdit.SelStart-myEdit.SelLength>Digits then Key:=#0;
end
else if (myEdit.Tag=2 ) then
begin
if (Digits>0) and (myEdit.SelStart>0) and (myEdit.SelLength=0) and (myEdit.Tag=2 ) then
if (pos('.',myEdit.Text )>0) and (myEdit.SelStart>=pos('.',myEdit.Text)) then
if length(myEdit.Text)-pos('.',myEdit.Text )>=Digits then Key:=#0;
end;
end;
end;