楼上正解 , 简单方法 先判断 是否为空 不是空的再加,
procedure TForm1.Button1Click(Sender: TObject);
var
result1,result2 ,i : integer;
begin
result1 := 0;
for i:=1 to 3 do
begin
if ( stringgrid1.Cells [i,1] = '' ) then
begin
button1.Enabled := false;
messagebox (Form1.Handle,'没有数字!' , 'error',mb_ok );
break;
end
else
result1:= result1 + strtoint(stringgrid1.Cells [i,1]);
end;
if i=4 then StringGrid1.Cells [4,1] := inttostr(result1) ;
result2 := 0;
for i:=1 to 3 do
begin
if ( stringgrid1.Cells [i,2] = '' ) then
begin
button1.Enabled := false;
messagebox (Form1.Handle,'没有数字!' , 'error',mb_ok );
break;
end
else
result2:= result2 + strtoint(stringgrid1.Cells [i,2]);
end;
if i=4 then StringGrid1.Cells [4,2] := inttostr(result2) ;
end;
由于没有输入数字 strtoint(stringgrid1.Cells [i,1]) 会触发异常,可用 StrToIntDef(stringgrid1.Cells [i,1],0)替代