怎样限制Edit只能输入数字(包括有小数点的数字)

怎样限制Edit只能输入数字(包括有小数点的数字) Frewin(frewin) Delphi VCL组件开发及应用 - delphi2007.net
http://www.delphi2007.net/DelphiVCL/html/delphi_2004010315571551964.html

procedure   TForm.Edit1KeyPress(Sender:   TObject;   var   Key:   Char);  
  begin  
          if   Key   in   ['0'..'9',#8,#13]   =   False   then  
          begin  
                  Application.MessageBox('只能输入数字!   ','系统提示',MB_OK+MB_ICONWARNING);  
                  Edit1.SetFocus;  
                  Abort;  
          end;  
  end;  
  //至于小数点,可以自己加控制

===============================

procedure   TForm1.Edit1KeyPress(Sender:   TObject;   var   Key:   Char);  
  begin      
      if   not(key   in   [#48..#57,#46])     then  
          Key   :=   #0;  
  end;

==============================

如果用键值去控制麻烦,不仅仅要看是不是数字和点,还要看是不是已经有了点,所以这样吧  
  try  
      strtofloat(edit1.text);  
  except  
      edit1.text:='0';  
  end

==================

procedure   TForm1.Edit1KeyPress(Sender:   TObject;   var   Key:   Char);  
  begin  
      if     not   (key   in   ['0'..'9','.',#8])   then  
          key:=#0  
  end;  

========================================

function   CheckInputNum(AStr:   string;   var   Key:   Char):   Boolean;  
      const   NumberSet   =   ['0'   ..   '9',   char(#8),   char(#13),   char(#46),   '.',   '-'];  
  begin                            
      if   not   (Key   in   NumberSet)   then   Key   :=   #0;  
      if   (Key   =   '.')   and   ((Length(AStr)   =   0)   or   (Pos('.',   AStr)   >   0))   then  
          Key   :=   #0;  
   
      if   (Length(AStr)   =   1)   and   (AStr[1]   =   '0')   and   (Key   =   '0')   then   Key   :=   #0;  
      Result   :=   Key   <>   #0  
  end;  
   
  你可以在OnKeyPress中调用    

============================================

  在EDIT   的ONEXIT(当这个EDIT失去焦点的时候)写几句话  
  try  
      strtofloat(edit.text);  
  except  
      showmessage('error');  
      edit.setfocus;  
  end;  
   
  还有就是在EDIT的ONKEYPRESS里添加对KEY的判断  
  if   not   (key   in   ['0'..'9','.',#8,#13])   then  
  key:=0;  
   
  上面这个写法有问题的,就是无法控制小数点的位置和个数.  
  所以在EDIT失去焦点的时候判断一下是很有必要的.除非你把错误的可能都排除了,禁止EDIT粘贴之类的,老问题了啊.期待高手写个这样EDIT的控件吧.呵呵,最好是免费的哦

  • fastReport3不知如何调整交叉报表的格式及显示位置?注意,是交叉报表!
  • 我写的这段代码,出现了上面的错,为什么,
  • treeview的难题?在线等,解决即给分
  • 关于TClientDataSet修改问题,有没有人知道修改TClientDataSet数据后记录不刷新的办法??
  • 急,导出注册表的问题
  • 怎样才能使edit中的字符串上下归中??
  • 如何取得record中的内容?
  • 请问如何释放自动化OLEVariant对象?
  • 请问$后加一些数字是什么类型的数据?
  • 没办法,高分求助一个问题(回答正确的还要加分),up者有分
  • posted on 2008-07-18 22:11  delphi2007  阅读(445)  评论(0编辑  收藏  举报