views:

54

answers:

1

I need to know when the row/column property of a grid was changed in order to do some processing. In TStringGrid Row property is

property Row: Longint read FCurrent.Y write SetRow;

But, unfortunately I cannot override the SetRow as it is private. SelectCell is not private BUT it is called BEFORE the new column and row attribute is set. The only solution will be to replace all calls to Row property with my own property

property MyRow: Longint read Row write SetMyRow;

but it is not the most elegant solution. Any ideas?


Delphi 7, Win 7 32 bit

+3  A: 

I just had a look at the source of TStringGrid. The Row property is inherited from TCustomGrid (via TDrawGrid and TCustomDrawGrid), where it is defined as

property Row: Longint read FCurrent.Y write SetRow;

as you say. SetRow calls FocusCell whichs calls MoveCurrent. This one calls SelectCell. This is a virtual function, and although it is highly trivial in TCustomGrid, where it is defined as

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

in TCustomDrawGrid, we have

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

Hence, OnSelectCell is called every time Row or Col is changed, as Skamradt wrote in a comment.

Yes, this event is called before the new cell is selected, but we have

FOnSelectCell: TSelectCellEvent;

where

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

ACol and ARow contain the new "values-to-be". You can even disallow the change of selected cell by setting CanSelect to false. Consequently, there is no need to override anything.

(Also, you cannot override SetRow because it is not virtual. It is very possible to override private and protected members, but only virtual methods can be overridden.)

Andreas Rejbrand
Thanks Andreas! Accepted.
Altar