嗨,我是Delphi的初学者.但令我困惑的是,我有Edit1.Text和变量"i",它使用StrToInt(Edit1.Text); 一切都好,直到我输入减号
如果我用数字复制/粘贴减号(例如-2)它可以工作任何人都可以帮助我!此致,奥马尔
StrToInt
当您不是100%确定输入字符串可以转换为整数值时,转换函数是不安全的.编辑框是一个不安全的情况.您的转换失败,因为您已将-
无法转换为整数的符号作为第一个char输入.清除编辑框时也会发生同样的情况.要使此转换安全,您可以使用该TryStrToInt
函数来处理转换异常.你可以这样使用它:
procedure TForm1.Edit1Change(Sender: TObject);
var
I: Integer;
begin
// if this function call returns True, the conversion succeeded;
// when False, the input string couldn't be converted to integer
if TryStrToInt(Edit1.Text, I) then
begin
// the conversion succeeded, so you can work
// with the I variable here as you need
I := I + 1;
ShowMessage('Entered value incremented by 1 equals to: ' + IntToStr(I));
end;
end;
Run Code Online (Sandbox Code Playgroud)