Search code examples
c++c++builderfiremonkeyc++builder-xe7tgrid

Highlighting TGrid row in code


I have a custom TGrid control which I am trying to make it so when the mouse hovers over a row, that row is highlighted. Rows are automatically highlighted if I used the arrow keys to navigate the grid. However, I am not sure how to replicate this effect for navigation with the mouse.

Currently, I have a MouseMove function which can detect which row of the grid the mouse is hovering over.

void __fastcall TFmSearchBar::GridMouseMove(TObject *Sender, TShiftState Shift, float X, float Y)
{
    int rowSelected = FGrid->RowByPoint(X, Y);

    if(rowSelected >= FGrid->RowCount)
        rowSelected = FGrid->RowCount - 1;

    if(rowSelected != -1)
    {
        FGrid->SelectRow(rowSelected);
    }
}

I originally thought using the SelectRow function would achieve the desired effect, however nothing happens when that method is used. Additionally I have tried using

FGrid->SelectCell(0, rowSelected);

which did not work either.

I have verified that I am getting the right row from the function by setting a row's text to bold when the mouse hovers over it using

FGrid->RowObjects[rowSelected]->SetBold();


Solution

  • you must enable following options for TGrid component:

    1) RowSelect = True 2) AlwaysShowSelection = True

    Tested with Delphi XE8 -- works fine. My code:

    procedure TForm1.Grid1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Single);
    begin
      Grid1.SelectRow(Grid1.RowByPoint(X, Y));
    end;
    

    if you want, i could provide you DFM file either.