Recent

Author Topic: MouseToCell  (Read 3839 times)

straetch

  • Jr. Member
  • **
  • Posts: 75
MouseToCell
« on: November 06, 2017, 01:04:36 pm »
Hello,
In my TWorksheetGrid I have an OnClick handler. The active cell is set to the cell that is clicked - as expected.
I also want to be able to browse the grid and right-click a cell. The handler should then give additional information on the cell. However, the cell should NOT become active in the grid.
So, I have a normal TWorksheetGridClick handler plus a TWorksheetGridMouseDown handler with a statement like     if Button = mbRight then ....
The problem is that I cannot derive the cell coordinates from the X and Y values passed to the handler.
The standard TStringGrid has a MouseToCell method, used as   ThisCell := StringGrid1.MouseToCell(X,Y).
TWorksheetGrid does not seem to have a MouseToCell method.
Is there an alternative?
Thanks.

Lazarus 1.6, FPSpreadsheet version dated june 17, 2017.

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: MouseToCell
« Reply #1 on: November 06, 2017, 05:25:06 pm »
Quote
TWorksheetGrid does not seem to have a MouseToCell method.

Yes it does.  The below compiles and works for me. 
Oh, I'm using Laz 1.9.  My copy of fpspreadsheet is circa March 2017.  Doubt the higher build of Laz would be an issue though, not for MouseToCell.

Code: Pascal  [Select][+][-]
  1. Var
  2.   mouseCell: TPoint;
  3.   r, c: Cardinal;
  4.  
  5. Begin
  6.   mouseCell := GRID.MouseToCell(Point(X, Y));
  7.   c := GRID.GetWorksheetCol(mouseCell.X);  
  8.   r := GRID.GetWorksheetRow(mouseCell.Y);  
  9. End

(Shamelessly ripped from the source code :-) )
« Last Edit: November 06, 2017, 05:28:30 pm by Mike.Cornflake »
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

straetch

  • Jr. Member
  • **
  • Posts: 75
Re: MouseToCell
« Reply #2 on: November 07, 2017, 09:16:47 am »
It does work.
Thanks.
I did not find the method in the api documentation. I could and should have tried it with TStringGrid as example.

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: MouseToCell
« Reply #3 on: November 07, 2017, 09:46:43 am »
In my TWorksheetGrid I have an OnClick handler. The active cell is set to the cell that is clicked - as expected.
I also want to be able to browse the grid and right-click a cell. The handler should then give additional information on the cell. However, the cell should NOT become active in the grid.
If I understand you correctly - this is what the WorksheetGrid is doing: if you right-click on a cell then the cell is *not* selected.

TWorksheetGrid does not seem to have a MouseToCell method.
The WorksheetGrid inherits from TCustomDrawGrid and, therefore, has all the methods that TCustomDrawGrid and TCustomGrid have. Since TStringGrid inherits from TCustomDrawGrid as well and thus follows a different lineage, TsWorksheetGrid does not share the methods and properties introduced by TStringGrid itself, but there's a good chance that most of the methods that you may know from TStringGrid will work for TsWorksheetGrid.

straetch

  • Jr. Member
  • **
  • Posts: 75
Re: MouseToCell
« Reply #4 on: November 07, 2017, 10:27:06 am »
Quote
If I understand you correctly - this is what the WorksheetGrid is doing: if you right-click on a cell then the cell is *not* selected.
Exactly, if you right-click a cell in a WorksheetGrid, it is not selected. That seems the correct way and that is what I was looking for: a way to give context to a user without activating anything.

Quote
The WorksheetGrid inherits from TCustomDrawGrid and, therefore, has all the methods that TCustomDrawGrid and TCustomGrid have.
Indeed, I looked only in the documentation of TsWorksheetGrid. I should have tried with the method, described for it's ancestor.

 

TinyPortal © 2005-2018