我需要知道网格的行/列属性何时更改才能进行某些处理。 在 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
(通过 TDrawGrid
和 TCustomDrawGrid
),其定义为
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;
因此,每次 Row
或 Col
更改时都会调用 OnSelectCell
,正如 Skamradt 在评论中所写。
是的,这个事件在选择新单元格之前调用,但是我们有
FOnSelectCell: TSelectCellEvent;
哪里
type
TSelectCellEvent = procedure (Sender: TObject; ACol, ARow: Longint;
var CanSelect: Boolean) of object;
ACol
和 ARow
包含新的“待定值”。您甚至可以通过将 CanSelect
设置为 false
来禁止更改所选单元格。因此,无需覆盖任何内容。
(此外,您无法重写SetRow
,因为它不是虚拟。很可能重写私有(private)和 protected 成员,但只能重写虚拟方法。)
关于delphi - TStringGrid 的 OnColumnChanged 和 OnRowChanged 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3981639/