I have a stringgrid that shows a bunch of files and information about those files. More info is shown about the current selected item in a separate panel. So, I want to know when the selected row changes to update the panel. OnSelectCell is not good because it triggers BEFORE the selection is actually moved to the new location. This is what I mean:
function TStrGrd.SelectCell(ACol, ARow: Longint): Boolean; {override}
begin
Result:= inherited SelectCell(ACol, ARow);
Mesage('Cur row: '+ IntToStr(row));
Mesage('New row: '+ IntToStr(ARow));
{ My own event }
if Assigned(FCursorChanged)
then FCursorChanged(Self); <-------- user will see the old row
end;
If the last row is selected and I click the first row, I will get these messages:
Cur row: 999
New row: 0
It would work if I create my own event handler and pass to it the row where the selection IS GOING TO be moved. It should work 100% but I am not very happy with this because the user would have to write some extra code in that event handler.
I could intercept all user interactions (mouse/key down) and all selection changes I do programatically but this requires quite a lot of code. There should be a more elegant way.
- This question is similar to What event fires every time a TDbGrid's selected location is changed? but not a duplicate. The answer to that question was 'use OnDataChange' but the TStringGrid doesn't have that event.
- @Andriy M suggests here why OnSelectCell won't work: Detecting single vs multiple selections in Delphi TStringGrid