3

Screen width is just not enough to display some text fields. I don't know how to auto-wrap them and I doubt that it can be easily done.

So, I thought that I would do something like

procedure TForm1.FormMouseMove(Sender: TObject;
    Shift: TShiftState; X,Y: Integer);

   var column, row : Integer;
begin
  myDbGrid.MouseToCell(X, Y, column, row);
  myDbGrid.Hinst := myDbGrid.Cells(column, row); // <==== ooops
end;

or, maybe do it in OnShowHint and get the mouse coords & translate them to column & row (more efficient)

but, of course, TDbGrid doesn't have Cells. Any idea how I can set the hint for the control as the user moves the mouse over the "cells" of the grid?

Arioch 'The
  • 15,799
  • 35
  • 62
Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551

2 Answers2

11

Use this code:

type
 THackGrid = class(TDBGrid);

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;
    finally
      THackGrid(DBGrid1).DataLink.ActiveRecord := ActRec;
    end;
  end;
end;
NGLN
  • 43,011
  • 8
  • 105
  • 200
1

This is code directly taken (albeit simplified) from a program of mine which displays as a hint one of the values of the dataset connected to the grid.

procedure TMainForm.DBGrid1MouseMove(Sender: TObject; Shift: TShiftState;
  X, Y: Integer);
var
 MousePos: TGridCoord; // X = Column, Y = Row

begin
 MousePos:= DBGrid1.MouseCoord (X, Y);
 if mousepos.X = 6  // we are over the 'tops' field
  then mainform.hint:= qPeopleTops.asstring;  // show for current person
end;
No'am Newman
  • 6,395
  • 5
  • 38
  • 50
  • what is "qPeopleTops" ? I suspect it to be a field, but how do you referecce it? myDbGrid.qPeopleTops? Or am I missing something? – Mawg says reinstate Monica Oct 25 '12 at 10:36
  • 1
    This code wouldnt work if the column would be resized, moved or any column would be made invisible.. – ertx Oct 25 '12 at 10:40
  • @Mawg: qPeople is the name of the dataset being displayed in the grid; qPeopleTops is the name of a persistent field of that dataset which presumably is displayed in column 6. – No'am Newman Oct 25 '12 at 10:49
  • Wouldn't that work only for the *active* record? is that what the OP wanted? – kobik Oct 25 '12 at 12:50
  • I think that when I edited the original code in order to present the essence here, I 'threw the baby out with the bathwater'. The actual production code displays a hint which displays an otherwise non-displayed field from the dataset, which changes as the mouse moves over the grid. – No'am Newman Oct 26 '12 at 04:15