在一个项目中,偶尔接到客户的反馈,当新增单据的时候DBGrideh涉及到日期格式选择的时候,偶尔会报除数为0(Division by zero)错误,一开始没有头绪,以为是操作系统兼容性问题,因为客户在操作步骤发生时没有涉及到任何数据的操作
终于实在受不了类似情况发生,跟踪了一把项目运行,发现DBGrideh源码文件GridsEH.pas中有这么一段
procedure TPopupListboxEh.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
var
BorderSize, TextHeight, Rows: Integer;
begin
BorderSize := GetBorderSize;
TextHeight := ItemHeight;
if TextHeight = 0 then TextHeight := GetTextHeight;
//作妖代码,TextHeight在赋值后,竟然还有可能存在为0的情况
Rows := (AHeight - BorderSize) div TextHeight;
if Rows < 1 then Rows := 1;
FRowCount := Rows;
inherited SetBounds(ALeft, ATop, AWidth, Rows * TextHeight + BorderSize);
end;
于是在作妖代码位置做了个为0判断
if TextHeight = 0 then TextHeight := 1;
最终代码如下:
procedure TPopupListboxEh.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
var
BorderSize, TextHeight, Rows: Integer;
begin
BorderSize := GetBorderSize;
TextHeight := ItemHeight;
if TextHeight = 0 then TextHeight := GetTextHeight;
//作妖代码再次做为0判断
if TextHeight = 0 then TextHeight := 1;
Rows := (AHeight - BorderSize) div TextHeight;
if Rows < 1 then Rows := 1;
FRowCount := Rows;
inherited SetBounds(ALeft, ATop, AWidth, Rows * TextHeight + BorderSize);
end;
至此问题消失