小弟需要文本框中只显示数字,写得一个相关函数。用异常来判断,为什么运行到strtoint(numstr);时,Delphi本身发出error,我写得异常处理要在关闭这个error提示才能运行,请高手看看是否有问题,谢谢!!!!!!
function tform5.isStrANumber(NumStr:string):boolean;
begin
result:=true;
try
  strtoint(numstr);
except
  on EConvertError do
  result:=false;
end;
end;

解决方案 »

  1.   

    function tform5.isStrANumber(NumStr:string):boolean;
    begin
    result:=true;
    try
      strtoint(numstr);
    exceptend;
    end;
      

  2.   

    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
      if (key > '9') or (key < '0') then
        if key <> #8 then
        key := #0;
    end;
      

  3.   

    function Tform5.isstranumber(NumStr:string):boolean;
    var i:integer;
    begin
      result:=true;
      for i:=0 to length(numstr) do
      if not (numstr[i] in ['0','1','2','3','4','5','6','7','8','9']) then
      begin
        result:=false;
        break;
      end;
    end;
      

  4.   

    用下列函数;返回0 是字符串,返回1是带小数的数字,2整数
    Function IsNumber(Str:String) : integer;
    var
       XSD , i : integer;
       cTmp : String;
    begin
       if (Str='') or (Str='+') or (Str='-') then begin Result := 0; exit ; end;
       XSD := pos('.',Str);
       if XSD >0 then Result := 1 else Result := 2;
       for i:=1 to length(Str) do begin
         cTmp :=copy(Str,i,1);
         if ((cTmp ='+') or (cTmp='-')) and (i>1) then Result := 0;
         if (cTmp='.') and (XSD<>i) then Result := 0;
         if (pos(cTmp,'+-0123456789.')<1) then Result := 0;
       end;
    end;
      

  5.   

    function tform5.isStrANumber(NumStr:string):boolean;
    begin
    result:=true;
    try
      strtoint(numstr);
    except
      result:=false;
    end;
    end;
      

  6.   

    function tform5.isStrANumber(NumStr:string):boolean;
    begin
    result:=true;
    try
      strtoint(numstr);
    except
      result:=false;
    end;
    end;