delphi - TStringGrid 单元格的范围检查是否有效?

标签 delphi delphi-7 tstringgrid

下面是一个简单的 Delphi 表单应用程序的代码,该应用程序设置超出包含单元格的指定 TStringGrid 范围的单元格值。

运行程序并单击显示表单上的结果网格,当计数器i超过 1 时,应该会生成运行时间范围检查错误。

在项目选项中启用了范围检查,并且我尝试过使用或不使用 {R+} 编译器指令来运行该程序。

为什么没有范围检查错误?

我使用的是在 Windows 7(64 位)上运行的 Delphi7。

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids;

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    procedure StringGrid1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
{$R+} 
procedure TForm1.StringGrid1Click(Sender: TObject);
var
    i : Integer;
begin
    Form1.StringGrid1.ColCount := 2;
    Form1.StringGrid1.RowCount := 3;
    for i := 0 to Form1.StringGrid1.RowCount do begin
        Form1.StringGrid1.Cells[0,i+1] := IntToStr(i);
    end;
end;

end.

最佳答案

来自documentation (添加强调):

The $R directive enables or disables the generation of range-checking code. In the {$R+} state, all array and string-indexing expressions are verified as being within the defined bounds, and all assignments to scalar and subrange variables are checked to be within range. If a range check fails, an ERangeError exception is raised (or the program is terminated if exception handling is not enabled).

TStringGrid 单元格引用不属于需要进行范围检查的变量和赋值类型。

关于delphi - TStringGrid 单元格的范围检查是否有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45403863/

相关文章:

delphi - 设置 TStringGrid 上选定行的背景颜色

delphi - 方法的默认通用比较器返回不正确的结果

multithreading - 仅处理最后一个TThread任务并丢弃先前的线程

delphi - 如何检测单击和双击 TreeView 中的节点?

delphi - 使用delphi以编程方式执行防病毒程序

delphi - 如何取消选择 FireMonkey TStringgrid 中的单元格

delphi - 接收 WM_PAINT 后,VCL/comctl32.dll/USER32.dll/GDI32.dll 中偶尔出现 EAccessViolation

delphi - 我想要更个性化的BalloonHint(背景颜色/圆角/透明度)

Delphi 2010 应用程序加载 Delphi 7 DLL

delphi - 从 OnDrawCell 事件外部绘制 TStringGrid 单元格,这可能吗?