Determine the right cell in TDrawgrid with mousecursor lazarus / 31-LCL ---------------------------------------------------------------------- mangakissa · 2014-10-30 11:07 In this topic I got showhint on every cell. But there's something strange. My TDrawgrid is indexed to 500 cels (20 cols * 25 rows). To determine the position of the mousecursor I had to use OnMouseMove and saved X and Y in a variabele. This peace of code will tel me on which cell my mousecursor is focused. Code: [Select]procedure TFrmPuzzelstukjes.PuzzelGridMouseMove(Sender: TObject;   Shift: TShiftState; X, Y: Integer); begin   fXas := X;   fYas := Y; end; procedure TFrmPuzzelstukjes.DoShowHint(Sender: TObject; HintInfo: PHintInfo); var ax,ay     : longint;     col, row  : integer;     celnumber : integer; begin   TDrawGrid(Sender).MouseToCell(HintInfo^.CursorPos.X,HintInfo^.Cursor Pos.Y,ax,ay);   col := fXas div 20;   row := fYas div 20;   celnumber := (row * 20) + col + 1;   HintInfo^.HintStr:= format('col (%d): %d, row (%d): %d, celnumber: %d',[fXas,col,fYas,row,celnumber]); end; But when my mousecursor is located on cell 20, it's telling me cell 25 is focused. A small calculation: 485 (X) div 20 (number of cols in a row) = 24. OnMouseMove is giving te right X position, because TDrawgrid.width is 504. How can I calculate the right index of the cell where the mousecursor is focused? ---------------------------------------------------------------------- Blaazen · 2014-10-30 11:21 IMO you are doing it wrong. You should calculate CellColumn = MouseX / WidthOfColumn. And it will work, of course, only if all columns has the same width and grid is not scrolled out. And the same is for rows. CellRow = MouseY / HeightOfRows. EDIT: I realized now that there's property GridLineWidth (default 1) which you should take into account too. ---------------------------------------------------------------------- wp · 2014-10-30 11:45 @mangakissa: Did you look at the source in grids.pas? Always a good idea... You'll see that TCustomGrid has a method for determining the rectangle enclosing a cell: Code: [Select]    function CellRect(ACol, ARow: Integer): TRect; ---------------------------------------------------------------------- mangakissa · 2014-10-30 15:20 @wp AFAIK CellRect is nice if you know the cell's col and row. You don't know it if you are using a mouse on it. @blaazen Thanks.  I knew I did something wrong. Just found out it should be this Code: [Select]col := fXas div 25; row := fYas div 25; Because the cell is 25 * 25 pixels. But your solution is much better and dynamic. ---------------------------------------------------------------------- wp · 2014-10-30 15:52 Quote if you know the cell's col and row.But you know that from MouseToCell which you are calling ?! ---------------------------------------------------------------------- User137 · 2014-10-30 17:11 Col, Row is in those ax, ay you ignored? ---------------------------------------------------------------------- mangakissa · 2014-10-31 07:23 I read the ax and yx and those gave me the range of the cell, not the col and row. But I solved it (thanks to Bart) with this: Code: [Select]TCustomGrid(Sender).MouseToCell(HintInfo^.CursorPos.X,Hi ntInfo^.CursorPos.Y,Col, Row); It seems property MouseToCell from TDrawgrid has a different approach than TCustomgrid. ---------------------------------------------------------------------- wp · 2014-10-31 08:28 QuoteIt seems property MouseToCell from TDrawgrid has a different approach than TCustomgrid.Nonsense - look at the source code: TDrawGrid inherits from TCustomDrawGrid which in turn inherits from TCustomGrid. MouseToCell is implemented only in TCustomGrid, therefore, TDrawGrid uses the same MouseToCell code as TCustomGrid. ---------------------------------------------------------------------- mangakissa · 2014-10-31 12:19 You're right. My apologies. I thought I saw different values but now I see the same (put a memo on screen to see what happens). ---------------------------------------------------------------------- wp · 2014-10-31 12:43 No need to apologize - sometimes we all don't see the obvious.