Board index » delphi » Cursor position in a cell of a TStringGrid

Cursor position in a cell of a TStringGrid

Hi,
I'm using a TStringGrid with 1 row and at the begining 1 column. The Cell
contains a string and i want that when the user double-click before a
character of the string, it split it into two cells (creating a new cell
dynamically) : the first with the begining of the string (before the cursor)
and the second with the rest.
Does anybody knows how i can trap the position of the cursor within a cell
on the double-click event ?
Thanks a lot in advance.
Robert_Mi...@NOSPAMCompuserve.com

 

Re:Cursor position in a cell of a TStringGrid


Look for the CellRect for position. If you want to know on which character of
the string the user clicked on, trap the mouseposition and look for
Canvas.TextWidth in the cell.

Daniel

Bob schrieb:

Quote
> Hi,
> I'm using a TStringGrid with 1 row and at the begining 1 column. The Cell
> contains a string and i want that when the user double-click before a
> character of the string, it split it into two cells (creating a new cell
> dynamically) : the first with the begining of the string (before the cursor)
> and the second with the rest.
> Does anybody knows how i can trap the position of the cursor within a cell
> on the double-click event ?
> Thanks a lot in advance.
> Robert_Mi...@NOSPAMCompuserve.com

Re:Cursor position in a cell of a TStringGrid


Quote
Bob wrote:

> Hi,
> I'm using a TStringGrid with 1 row and at the begining 1 column. The Cell
> contains a string and i want that when the user double-click before a
> character of the string, it split it into two cells (creating a new cell
> dynamically) : the first with the begining of the string (before the cursor)
> and the second with the rest.
> Does anybody knows how i can trap the position of the cursor within a cell
> on the double-click event ?
> Thanks a lot in advance.
> Robert_Mi...@NOSPAMCompuserve.com

I did something like that, using Delphi 1.  The first step is to derive
a new component from tStringGrid.  Note that some of what I'm including
here is irrelevant to you; it just comes with the copying, and I'm too
busy to edit it out:

   tEnhancedGrid=class(tStringGrid)
    private  {MoveColRow was formerly protected, not private}
      LeftMessage:          tWMLbuttondown;
      MouseReGrid,
      MouseReScreen:        tPoint;
      MouseCol,
      MouseRow:             longint;
      MouseCellRect:        tRect;
      LastMouseButton:      tMouseButton;
      AssociatedPage:       tGridPageInstance;
      procedure MoveColRow(ACol,ARow: integer);
    protected
      procedure TranslateClick(var Message: tMessage);
      procedure CNLButtonDown(var Message: TWMLbuttondown);
                              message WM_LBUTTONDOWN;
      procedure CNLButtonDblClk(var Message: TWMLButtonDblClk);
                              message WM_LBUTTONDBLCLK;
      procedure CNRButtonDown(var Message: TWMRbuttondown);
                              message WM_RBUTTONDOWN;
      end;

I was watching for a left click; I assume you'll get a left-click
message before you get a double-click message:

procedure tEnhancedGrid.TranslateClick(var Message: tMessage);
begin
   with TWMLbuttonDown(Message) do begin
      MouseReGrid.X:=XPos;
      MouseReGrid.Y:=YPos
      end;
   MouseReScreen:=Self.ClientToScreen(MouseReGrid);
  {The help file is wrong! MouseToCell works in co-ordinates relative to
   the grid, not to the screen!}
   MouseToCell(MouseReGrid.X,MouseReGrid.Y,MouseCol,MouseRow);
   MouseCellRect:=CellRect(0,MouseRow);
   if (MouseCellRect.Bottom<MouseReGrid.Y) then
      MouseRow:=maxint {below the last row used}
end;

procedure tEnhancedGrid.CNLbuttondown(var Message: TWMLbuttondown);
begin
   LeftMessage:=message;
   TranslateClick(tMessage(Message));
   LastMouseButton:=mbLeft;
end;

Anyway, this should be enough to get you started.

--
Howard L. Kaplan
Psychopharmacology and Dependence Research Unit
Women's College Hospital
76 Grenville Street, 9'th floor
Toronto, Ontario
Canada  M5S 1B2
(416)323-6400, ext 4915
howard.kap...@utoronto.ca

Other Threads