Recent

Author Topic: Detecting the Row and Col of a StringGrid that has a blank space?  (Read 833 times)

Jvan

  • Full Member
  • ***
  • Posts: 181
Hi.

I have the next code to detect the Row and Col of a cell in which the pointer is over.

Code: Pascal  [Select][+][-]
  1. procedure TfrmMain.myGridMouseMove(Sender: TObject; Shift: TShiftState;
  2.   X, Y: Integer);
  3. var
  4.    Col, Row: Integer;
  5. begin
  6.   myGrid.MouseToCell(X, Y, Col, Row);
  7.   ShowMessage('Row: ' + intToStr(Row) + ' | Col: ' + intToStr(Col));
  8. end;
  9.  

But my grid have a few rows and a higher height, so there is a blank space, and when the mouse is over that space it shows me the number of the last row.

I only want to get the Row and Col number when the mouse is exactly over cell.
« Last Edit: August 11, 2020, 11:25:42 pm by Jvan »

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Detecting the Row and Col of a StringGrid that has a blank space?
« Reply #1 on: August 11, 2020, 11:21:21 pm »
if that is the case then I would say it's a bug in the LCL code..

but by looking at your code example, you are using a different control to perform this query so
the bug could be on your side actually..

What widget are you on? (Target)

The only true wisdom is knowing you know nothing

Jvan

  • Full Member
  • ***
  • Posts: 181
Re: Detecting the Row and Col of a StringGrid that has a blank space?
« Reply #2 on: August 11, 2020, 11:35:54 pm »
When I said "blank space" what I mean is that I have 4 rows, for example, and my grid has "Height" (align := allClient). So there is a "free space" on the grid, the "height" of all the rows are lower than the height of the grid. And I want to get the Row and Col numbers when the mouse is over a cell, but not over that free space.

wp

  • Hero Member
  • *****
  • Posts: 11853
Re: Detecting the Row and Col of a StringGrid that has a blank space?
« Reply #3 on: August 11, 2020, 11:44:38 pm »
After determining the cell's row and col indices you can determine the cell's rectangle and check the mouse coordinates against the cell's right and bottom borders:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
  2.   Y: Integer);
  3. var
  4.   c, r: Integer;
  5.   rct: TRect;
  6. begin
  7.   StringGrid1.MouseTocell(X, Y, c, r);
  8.   rct := StringGrid1.CellRect(c, r);
  9.   if (Y > rct.Bottom) or (X > rct.Right) then
  10.     Caption := 'no cell'
  11.   else
  12.     Caption := Format('Col = %d, Row = %d', [c, r]);
  13. end;  
« Last Edit: August 11, 2020, 11:47:40 pm by wp »

Jvan

  • Full Member
  • ***
  • Posts: 181
Re: Detecting the Row and Col of a StringGrid that has a blank space?
« Reply #4 on: August 11, 2020, 11:52:32 pm »
Thanks!

 

TinyPortal © 2005-2018