Question

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?

Was it helpful?

Solution

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;

OTHER TIPS

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top