如何让Edit只能输入数字?用MaskEdit好像只能输入规定的位数。

解决方案 »

  1.   

    1第三方控件
    2在keypress中判断
      

  2.   

    同意larruping(手持河图,怀揣洛书) 
    通过keypress事件过滤非数字
      

  3.   

    同意larruping(手持河图,怀揣洛书) 
    通过keypress事件过滤非数字
      

  4.   

    通过keypress事件过滤非数字:
    if not (key in ['0'..'9',#13,#8] then
      showmessage('');
    如果想规定输入几个数字的话可以在属性里面设置length为n。
      

  5.   

    //在该Edit的KeyPress事件里加入如下代码:
    begin
      if not (Key in ['0'..'9', #9, #13, #8]) then Key := #0;
      //other code
    end;
      

  6.   

    var
       i:Byte;
    begin
       i:=strtoint(Edit1.text);
       if (i in [0..65535]) then
           showmessage('')
       else
           showmessage();
    end;
      

  7.   

    同意edit的keypress过虑数字
     
      

  8.   

    最完善的解决方案建议参考TDBEdit中有关数据类型是数字时的输入限制。
    比如,楼上关于KeyPress过滤,如果我输入负号'-'或者'2e3'或者'.1',算不算数字呢?
    如果输入'-1-1'又怎么办呢?如果要用方向键移动光标呢?...
      

  9.   

    因此,必需充分考虑:
    只允许输入整数(又分为是否运行小于0);
    允许输入整数和实数(又分为是否运行小于0);
    ...
    需要考虑的太多了。
    因此,俺通常在用户输入确认时再检查是否合法(用try...except),省去一大堆麻烦。
      

  10.   

    在onpress中加
    if not (Key in ['0'..'9', #13, #8]) then Key := #0;
    即可
    #13,#8表可回车可退格要是没#8会不能删自符
      

  11.   

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

  12.   

    //在该Edit的KeyPress事件里加入如下代码:
    begin
      if not (Key in ['0'..'9', #9, #13, #8]) then Key := #0;
      //other code
    end;
      

  13.   

    我认为cnsuyong的方法最好!可以试试。
      

  14.   

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

  15.   

    if not (key in ['0'..'9',#13,#8] then
      

  16.   

    不用考虑就是这个了。
    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
            if not (Key in ['0'..'9', #13, #8]) then
            Key := #0;end;
      

  17.   

    有这么繁杂吗,只要把IMEMODE里面设置为IMCLOSE就可以了
      

  18.   

    楼上的在鬼扯啥子,imemode是啥子东东哦。
      

  19.   

    用Keypress好象是可以, 不过粘贴时就没用了.所以根本就不是一个好的方法.
      

  20.   


    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
         if  not isnumberic(key) then
           begin
             showmessage('提示信息');
             key:=char(0);         
           end;
    end;
         同时可以单独加入判断回车等其他键;isnumberic()是系统提供的函数;