Well, you could override DoMouseWheelDown
to achieve this.
function TMyStringGrid.DoMouseWheelDown(Shift: TShiftState;
MousePos: TPoint): Boolean;
begin
if Row<RowCount-2 then
//only allow wheel down if we are above the penultimate row
Result := inherited DoMouseWheelDown(Shift, MousePos)
else
Result := False;
end;
But how do you know that there isn't some other way to move the focus to the last row?
In fact a much better solution is to override SelectCell
:
function TMyStringGrid.SelectCell(ACol, ARow: Longint): Boolean;
begin
Result := ARow<RowCount-1;
end;
When you do it this way you don't need any KeyDown
code, and you don't need to override DoMouseWheelDown
. All possible mechanisms to change the selected cell to the final row will be blocked by this.
As @TLama correctly points out, you don't need to sub-class TStringGrid
to achieve this. You can use the OnSelectCell
event:
procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol, ARow: Longint;
var CanSelect: Boolean);
begin
CanSelect := ARow<(Sender as TStringGrid).RowCount-1;
end;