如何限制一个edit控件中只能输入数字,急急急急急急急急急急急急
如何限制一个edit控件中只能输入数字,急急急急急急急急急急急急

解决方案 »

  1.   

    在edit的onChange事件中输入以入代码;
    var  
      v: double;
      code: integer;
      numstr: string;
    begin
      numstr:= edit.text;
      val(numstr,v,code);
      if code > 0 then
      begin
        if length(numstr)=1 then
           Delete(numstr,code,1)
        else if numstr <> '-' then//如果输入的第一字符不是'-'
           Delete(numstr,code,1);  end;
      edit.text:= numstr;
    end;
      

  2.   

    为什么不用dbedit,数据类型为数值型,自动不允许录入基他东西.
      

  3.   

    这是我原来写的一个一个过程,只能输入数字键和字母键和“.”
    {键盘输入控制}
    procedure KeyControl(Keyset:Char);
    var
      handle:HWND;
     flagNum,flagXX,flagDX:boolean;
    beginflagNum:=((Keyset>'9') or (Keyset<'0'));
    flagXX:=((Keyset>'z') or (keyset<'a'));
    flagDX:= ((Keyset>'Z') or (keyset<'A'));
    if (flagNum and flagXX and flagDX and (Keyset<>#8) and (Keyset<>'.')) then
         begin
           messagebox(handle,pchar('该按键此处禁用!'),pchar('信息'),mb_ok);
          Abort;
          end;
    end;
      

  4.   

    在onkeypress中输入if (key>='0')or(key<='9') then
    key:=#0;
      

  5.   

    to 大青虫
      你说的方法不行
      如果用户输错怎么办?
     也不能删除
     if not(key in ['0'..'9']) then
      begin
          if not(key =#8) then
          begin
            application.MessageBox('  输入必须为数字 !','提示',MB_ICONWARNING+MB_OK);
            key:=chr(0);
          end;
      end;这样的话输入错误后还可以删除
    你试一下
      

  6.   

    procedure TQueryBilForm.ComPTextKeyPress(Sender: TObject; var Key: Char);
    begin
      if Assigned(CurField) and(CurField.DataType in [ftFloat,ftCurrency,ftBCD]) then
          if not((key in ['0'..'9','.',#8])) then
            key := #0
          else
            begin
              if (Key='.') and (Pos('.',TEdit(Sender).Text)<>0) then
                 Key:=#0;
            end;
    end;
    小数点只能有一个,可以backspace!
      

  7.   

    补充上面的,
     if not((key in ['0'..'9','.',#8]) or key =#13) then
      

  8.   

    用异常处理来做:
       try
         StrtoFloat(Edit1.Text);
       except
         On EConvertError do
         ShowMessage('必须是数字呀!');
       end;
      

  9.   

    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    var l:boolean;
    begin
    l:=(key<#8)or(key>#8)and (key<#45)or(key>#46)and(key>#57);
    if l then key:=#0;end;
      

  10.   

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

  11.   

    为什么不用
    MarkEdit
    inputMark:9 0到一个数字
    inputMarK:0 一个数字
      

  12.   

    不用这么麻烦呵,
    设定一下TEdit的样式则可:var dwStyle:LongInt;dwStyle := GetWindowLong(Edit1.Handle, GWL_STYLE);
    SetWindowLong(Edit1.Handle, GWL_STYLE, dwStyle or ES_NUMBER);请试用我的软件
    www.netgocn.com
      

  13.   

    //判断字符串
    function judtext(smsText:String):Boolean;
    var
       i:shortint;                            //循环
       judnorm:smallint;                      //判断基准
    begin
       Result:=True;
       for i:=1 to length(smsText) do
       begin
          judnorm:=Byte(smsText[i]);                           //取得每一个字符的asc码
          if (judnorm<48) or (judnorm>57) then 
          begin
             Result:=False;
             break;
          end;
       end;end;
      

  14.   

    //当edit1中不是一个有效数字时,把它值赋为0
    procedure TForm1.Edit1Change(Sender: TObject);
    begin
      Edit1.Text:=IntToStr(StrToIntDef(Edit1.Text,0));
    end;
      

  15.   

    在Edit的OnKeyPress内
      if not (Key in ['0'..'9','.',#13,'-',#8]) then {#13回车键,#8BackSpace键} 
        Key:=#0;
      

  16.   

    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
      if ((key<'0') or (key>'9')) and (key<>#13) and (key<>'.') and (key<>#8) then
      begin
         Application.MessageBox('','');
         abort;
      end;
    end;
    刚试过,很好用的。
    多谢楼上各位的指点。
      

  17.   

    Edit 中只输入数字
        SetWindowLong(Edit1.Handle, GWL_STYLE,
                      GetWindowLong(Edit1.Handle, GWL_STYLE) or
                      ES_NUMBER);
      

  18.   

    onKeyPress(Sender: TObject; var Key: Char);
      
    //     BackSpace退格键 :#8
      if not ((Key=#8)or((key>='0')and(key<='9'))) then
        key := #0 ;
      

  19.   

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

  20.   

    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    var l:boolean;
    begin
    l:=(key<#8)or(key>#8)and (key<#45)or(key>#46)and(key>#57);
    if l then key:=#0;end;
      

  21.   

    if Edit1.Text <> '' then
      begin
        s := Edit1.Text;
        for i := 1 to Length(s) do
        begin
          if (s[i] in ['0'..'9']) then
            Continue
          else
          begin
            Edit1.SetFocus;
            Edit1.SelectAll;
            MessageDlg('Invalide Num!',mtInformation,[mboK],0);
            exit;
          end;
        end;
      end
      else
      begin
        Edit1.SetFocus;
        MessageDlg('No Num!',mtInformation,[mboK],0);
        Exit;
      end;
      

  22.   

    OnKeyPress事件中处理:if key not in ['0'..'9'] then
      Key:= #0;
      

  23.   

    firetoucher的这一段很直接,要uses windows
    Edit 中只输入数字
        SetWindowLong(Edit1.Handle, GWL_STYLE,
                      GetWindowLong(Edit1.Handle, GWL_STYLE) or
                      ES_NUMBER);
    不过这样也行:procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
      if not (key in['0'..'9',#13,#8])
      then
      begin
        key:=chr(0);
        showmessage('输入错误,请输入数字');
      end;
    end;
    其实楼上的已经不能再完善了,还不结贴!
      

  24.   

    又来晚一步,咳ffwin()的最好
      

  25.   

    在Edit的OnKeyPress内
      if not (Key in ['0'..'9','.',#13,'-',#8]) then {#13回车键,#8BackSpace键} 
        Key:=#0;
    不对,不对  belllab(bell) 的最好