bobonwhidbey
bobonwhidbey

Reputation: 505

Selectively ShowHint with StringGrid

In a StringGrid component descendant, I want to change the popup Hint message depending on the value of a cell. My coding:

procedure TForm.GridMouseEnterCell(Sender: TObject; ACol, ARow: Integer);
var  k: integer;
begin
  k := strtointdef(Grid.Cells[13, ARow],-1);
  Grid.ShowHint := (ACol = 12) and (k >= 0);
  if Grid.ShowHint then
    Grid.Hint := MyLIst.Items[k];
 end;

This works fine when I mouse over to Col 12 from another column, but if stay in col 12 and move to another row (with a different value of k), the popup hint does not change. It will only show the correct/new hint when I first mouse over to another column, and then back to col 12. Anyone have a solution?

Upvotes: 4

Views: 3738

Answers (2)

David Heffernan
David Heffernan

Reputation: 612794

The cleanest way to modify the hint at runtime is to intercept the CM_HINTSHOW message. Doing it this way means that you don't need to hunt down all the different events that might lead to your hint changing. Instead you simply wait until the hint is about to be shown and use the current state of the control to determine what to show.

Here's an example using an interposer class:

type
  TStringGrid = class(Grids.TStringGrid)
  protected
    procedure CMHintShow(var Message: TCMHintShow); message CM_HINTSHOW;
  end;

procedure TStringGrid.CMHintShow(var Message: TCMHintShow);
var
  HintStr: string;
begin
  inherited;
  // customise Message.HintInfo to influence how the hint is processed
  k := StrToIntDef(Cells[13, Row], -1);
  if (Col=12) and (k>=0) then
    HintStr := MyList.Items[k]
  else
    HintStr := '';
  Message.HintInfo.HintStr := HintStr;
end;

If you wanted to make this more useful you would derive a sub-class of TStringGrid and add on OnShowHint event that allowed such customisation to be specified in a less coupled manner.

Upvotes: 4

rsrx
rsrx

Reputation: 1473

Are you sure that OnMouseEnterCell() event is working properly? Does it gets called once you stay in column and move to another row? Since it's event of a descendant, and not of a TStringGrid, we don't have insight in it.

Also, try putting Application.ActivateHint(Mouse.CursorPos); at the end of your function. It will force hint to reshow:

procedure TForm.GridMouseEnterCell(Sender: TObject; ACol, ARow: Integer);
var
  k: integer;
begin
  k := StrToIntDef(Grid.Cells[13, ARow], -1);
  Grid.ShowHint := (ACol = 12) and (k >= 0);
  if Grid.ShowHint then
  begin
    Grid.Hint := MyList.Items[k];
    Application.ActivateHint(Mouse.CursorPos);
  end;
end;

Upvotes: 0

Related Questions