简体   繁体   中英

OnColumnChanged and OnRowChanged events to TStringGrid

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

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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM