把edit中的内容转成整形  strtoint
如果输入是英文就会出错,请问用哪个异常处理

解决方案 »

  1.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      i:integer;
    begin
      Try
      begin
        i:=StrToInt(Edit1.Text);
      end
      Except
        ShowMessage('Wrong!');
      end;
    end;
      

  2.   

    可以使用delphi提供的val方法:
    var   I, Code: Integer;
    begin
      { Get text from TEdit control }
      Val(Edit1.Text, I, Code);
      { Error during conversion to integer? }
      if Code <> 0 then
        MessageDlg('Error at position: ' + IntToStr(Code), mtWarning, [mbOk], 0);
      else
        Canvas.TextOut(10, 10, 'Value = ' + IntToStr(I));end;
      

  3.   

    如果我使用strtofloat那小数点也算错误吗
      

  4.   

    Ciny_Cou这种方法我试过,不行
      

  5.   

    if StrToIntDef('aaa',-4321)=-4321 then ShowMessage('Error!')
      

  6.   

    if StrToIntDef('aaa',-4321)=-4321 then ShowMessage('Error!')
    else i:=StrToIntDef('aaa',-4321)function StrToIntDef(const S: string; const Default: Integer): Integer;C++ syntax:extern PACKAGE int __fastcall StrToIntDef(const AnsiString S; const int Default);DescriptionStrToIntDef converts the string S, which represents an integer-type number in either decimal or hexadecimal notation, into a number. If S does not represent a valid number, StrToIntDef returns Default.