delphi中StringGrid.Cells如果没有输入数字,怎么显示一个提醒的窗口???

2025-03-01 22:22:25
推荐回答(2个)
回答1:

楼上正解 , 简单方法 先判断 是否为空 不是空的再加,
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;

回答2:

由于没有输入数字 strtoint(stringgrid1.Cells [i,1]) 会触发异常,可用 StrToIntDef(stringgrid1.Cells [i,1],0)替代