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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With