• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1847
  • Last Modified:

How to set the Stringgrid hint in response to mouseover?

Hi Folks!

I  have a TStringgrid measuring 20 cols and 20 rows, each filled with a character ranging from A to Z, loaded from a 400 byte textfile.  Now I want the Stringgrids hint to reflect the position of the mouse as if the Stringgrid cells were numbered from left to right top to bottom in the range of 1 to 400.
To make things more clear > the first row should read cells 1 to 20, the second row should read cells 21 to 40 and so on. So when I move my mouse from left to right over the first row the hints 1 to 20 should be displayed shortly after another. Instead of the hint displaying its 'position' a label or edit may also reflect the location.
i just need to know the exact location by means of the 400 positions on which I base the textfile.

If you need more info let me know,

Regards Peter

Ps if you think another approach would be more appropriate then I'm all ears as well. Providing working sample will help you another step on that huge huge list of experts out here on EE :)
0
PeterdeB
Asked:
PeterdeB
  • 2
1 Solution
 
mokuleCommented:
OK
Here it is

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids;

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    procedure StringGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    th: THintWindow;
    ColPrev,RowPrev: integer;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  ColPrev := -1;
  RowPrev := -1;
  th := THintWindow.Create(self);
  th.Parent := self;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  th.Free;
end;

procedure TForm1.StringGrid1MouseMove(Sender: TObject; Shift: TShiftState;
  X, Y: Integer);
var
  pt: TPoint;
  Col,Row: integer;
begin
  pt := StringGrid1.ClientToScreen(Point(X,Y));
  StringGrid1.MouseToCell(X,Y,Col,Row);
  if (Col <> ColPrev) or (Row <> RowPrev) then
    begin
    ColPrev := Col;
    RowPrev := Row;
    if (Col >= 0) and (Col <= 20) and (Row >= 0) and (Row <= 20) then
      th.ActivateHint(Rect(pt.X+20,pt.Y-8,pt.X+50,pt.Y+8),IntToStr(Col+1+20*Row));
    end;
end;

end.
0
 
mokuleCommented:
You can also add timer to hide the hint when the mouse is out of StringGrid

procedure TForm1.Timer1Timer(Sender: TObject);
var
  pt: TPoint;
begin
  pt := StringGrid1.ScreenToClient(Mouse.CursorPos);
  if (pt.X < 0) or (pt.Y < 0) or (pt.X > StringGrid1.Width) or (pt.Y > StringGrid1.Height) then
    th.ReleaseHandle;
end;
0
 
PeterdeBAuthor Commented:
Simp-ly Per-fect!!

Muchos Gracias Mokule!!

I decided to comment it myself btw >

//
 if (Col >= 0) and (Col <= 20) and (Row >= 0) and (Row <= 20) then  // if mouse dares to enter stringgrid
      th.ActivateHint(Rect(pt.X+20,pt.Y-8,pt.X+50,pt.Y+8),IntToStr(Col+1+20*Row)); // scare it off with some rocket science!! :)
    end;
//

Regards Peter :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now