让edit中只输入数字并且只能输入两位小数(20分)

  • 主题发起人 主题发起人 shankedriver
  • 开始时间 开始时间
S

shankedriver

Unregistered / Unconfirmed
GUEST, unregistred user!
让edit中只输入数字并且只能输入两位小数
 
使用tMaskEdit,同时设置editmask成!999.99;1;_
 
同意楼上的观点~~
 
我修改了别人的一个函数, 用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;
 
不觉得问题说清楚了.
是否只让用户输入:0.XX呢?
 
后退
顶部