How to show DBGrid.Hint when hovering with the mouse on a cell?

delphi

Solution

Call `Application.ActivateHint` with the desired coordinates for the hint, e.g.

Procedure ChangeHint(C: TControl; Const Hint: String; p: TPoint);
var
  OldHint: String;
begin
  OldHint := C.Hint;
  if Hint <> OldHint then
  begin
    C.Hint := Hint;
    Application.ActivateHint(p);
  end;
end;

procedure TForm3.DBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
  ...
  ChangeHint(TDBGrid(Sender), YourIntendedHintText, TDBGrid(Sender).ClientToScreen(Point(X, Y)));
  ....
end;

Problem

While the following code does correctly set the Form1.Caption with the text of the cell the mouse is over, it does not display any DBGrid.Hint unless I click on the cell. What is wrong with this picture? ``` type THackGrid = class(TDBGrid); // to expose protected DataLink property procedure TForm1.DBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); var Cell: TGridCoord; ActRec: Integer; begin Cell := DBGrid1.MouseCoord(X, Y); if dgIndicator in DBGrid1.Options then Dec(Cell.X); if dgTitles in DBGrid1.Options then Dec(Cell.Y); if THackGrid(DBGrid1).DataLink.Active and (Cell.X >= 0) and (Cell.Y >= 0) then begin ActRec := THackGrid(DBGrid1).DataLink.ActiveRecord; try THackGrid(DBGrid1).DataLink.ActiveRecord := Cell.Y; Caption := DBGrid1.Columns[Cell.X].Field.AsString; // Form1.Caption shows fine! DBGrid.Hint := DBGrid1.Columns[Cell.X].Field.AsString; // <== Hint only shows when I click into the cell! finally THackGrid(DBGrid1).DataLink.ActiveRecord := ActRec; end; end; end; ```

Original source