delphi - TStringGrid 的 OnColumnChanged 和 OnRowChanged 事件

标签 delphi delphi-7

我需要知道网格的行/列属性何时更改才能进行某些处理。 在 TStringGrid 中 Row 属性是

property Row: Longint read FCurrent.Y write SetRow;

但是,不幸的是我无法覆盖 SetRow,因为它是私有(private)的。 SelectCell 不是私有(private)的,但在设置新的列和行属性之前调用它。唯一的解决方案是将所有对 Row 属性的调用替换为我自己的属性

property MyRow: Longint read Row write SetMyRow;

但这不是最优雅的解决方案。 有什么想法吗?


Delphi 7、Win 7 32 位

最佳答案

我刚刚查看了TStringGrid的源代码。 Row 属性继承自 TCustomGrid(通过 TDrawGridTCustomDrawGrid),其定义为

property Row: Longint read FCurrent.Y write SetRow;

正如你所说。 SetRow 调用 FocusCell,后者又调用 MoveCurrent。这个调用 SelectCell。这是一个虚函数,尽管它在 TCustomGrid 中非常简单,但它的定义为

function TCustomGrid.SelectCell(ACol, ARow: Longint): Boolean;
begin
  Result := True;
end;

TCustomDrawGrid中,我们有

function TCustomDrawGrid.SelectCell(ACol, ARow: Longint): Boolean;
begin
  Result := True;
  if Assigned(FOnSelectCell) then FOnSelectCell(Self, ACol, ARow, Result);
end;

因此,每次 RowCol 更改时都会调用 OnSelectCell,正如 Skamradt 在评论中所写。

是的,这个事件在选择新单元格之前调用,但是我们有

FOnSelectCell: TSelectCellEvent;

哪里

type
  TSelectCellEvent = procedure (Sender: TObject; ACol, ARow: Longint;
var CanSelect: Boolean) of object;

AColARow 包含新的“待定值”。您甚至可以通过将 CanSelect 设置为 false 来禁止更改所选单元格。因此,无需覆盖任何内容。

(此外,您无法重写SetRow,因为它不是虚拟。很可能重写私有(private)和 protected 成员,但只能重写虚拟方法。)

关于delphi - TStringGrid 的 OnColumnChanged 和 OnRowChanged 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3981639/

相关文章:

delphi - 如何同步父/子进程执行?

Delphi VCL 样式教程 - 如何在运行时更改样式

delphi - 如何检测本地网络中其他设备的IP地址?

delphi - 使已开发的应用程序启用 64 位

delphi - 始终在 TValueListEditor 中显示选项下拉列表

delphi - 从 BPL 函数返回字符串

delphi - 动态创建的对象(以字符串形式提供其类名)不调用其构造函数

png - 如何知道 TPNGObject 是否具有有效的 header ?

.net - 在 GetKeyboardType() Windows API 调用中 Delphi 6 程序启动期间发生访问冲突

wcf - 如何使用 Delphi 2010 导入 https webservice (wcf)?