Recent

Author Topic: PopupMenu on a selected cell of a DBGrid  (Read 4182 times)

dsyrios

  • Jr. Member
  • **
  • Posts: 57
PopupMenu on a selected cell of a DBGrid
« on: January 07, 2019, 06:41:05 pm »
Hi,
I want to activate a PopupMenu over/nearby a selected cell on a DBGrid, using Right button click.
I use the below code and it's ok about the proper column.
But the PopupMenu is triggering all over the column, not only the selected cell.
Maybe the OnMouseUp is not the right one procedure.
Any helping tip?
Code: Pascal  [Select][+][-]
  1. procedure TForm1.DBGrid1MouseUp(Sender: TObject; Button: TMouseButton;
  2.                                           Shift: TShiftState; X, Y: Integer);
  3. var
  4.   aCol, aRow : integer;
  5. begin
  6.  if Button = mbRight then
  7.    begin
  8.      TDBGrid(Sender).MouseToCell(X,Y,aCol,aRow);
  9.      if (aCol=2)
  10.         { AND
  11.           aRow=???   <-- I need only the row of the selected cell
  12.         }
  13.        then begin
  14.           TDBGrid(Sender).PopupMenu.Popup;
  15.        end;
  16.    end;
  17. end;
  18.  
Laz 2.0.2/FPC 3.0.4/ win10/64
Laz 2.0.0/FPC 3.0.4/ mint-mate 19.1

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: PopupMenu on a selected cell of a DBGrid
« Reply #1 on: January 07, 2019, 07:03:34 pm »
The only helping tip is that you uncomment the "and aRow = "; as it is now it will pop-up everytime aCol = 2 ... which, of course, happens over all the column.

But maybe I misunderstood the question?

Also, if the popup menu is owned by the grid, doen't it pop-up any time you right-click anywhere inside it?
« Last Edit: January 07, 2019, 07:06:25 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

dsyrios

  • Jr. Member
  • **
  • Posts: 57
Re: PopupMenu on a selected cell of a DBGrid
« Reply #2 on: January 07, 2019, 08:35:18 pm »
@lukamar,
thanks for the reply
Maybe I wasn't clear enough.
The PopupMenu is triggering all over the column with right click,
I want to catch the SelectedCell TPoint(X,Y) coords NOT mouse coords
so to do
TDBGrid(Sender).PopupMenu.Popup(SelectedCell.X,SelectedCell.Y)
Laz 2.0.2/FPC 3.0.4/ win10/64
Laz 2.0.0/FPC 3.0.4/ mint-mate 19.1

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: PopupMenu on a selected cell of a DBGrid
« Reply #3 on: January 07, 2019, 08:49:07 pm »
Let me test my understanding: your real problem is that you don't know the row number of the Selected cell, so you can't test for it in the OnMouseUp handler. Is that it?

If so, let me check a couple things; I think I've seen something like this before ... ETA: got it! Not the most obvious thing in the world, but use Grid.SelectedRows; it should return just one bookmark unless there are more than one row selected.

Incidentally ... why aren't you using OnCellClick?
« Last Edit: January 07, 2019, 08:56:06 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

dsyrios

  • Jr. Member
  • **
  • Posts: 57
Re: PopupMenu on a selected cell of a DBGrid
« Reply #4 on: January 08, 2019, 04:37:55 am »
@lukamar,
thanks for your attention.
FYI, the below code maybe is somehow tricky, but now seems to works perfect for me.

Code: Pascal  [Select][+][-]
  1. implementation
  2. ...
  3. type
  4.   TDummyDBGrid=class(TDBGrid);
  5. ...  
  6. procedure TForm1.DBGrid1MouseUp(Sender: TObject; Button: TMouseButton;
  7.                                           Shift: TShiftState; X, Y: Integer);
  8. var
  9.   aCol, aRow, xRow : integer;
  10. begin
  11.  xRow := TDummyDBGrid(TDBGrid(Sender)).Row;          { xrow  from DBGrid }
  12.  TDBGrid(Sender).MouseToCell(X,Y,aCol,aRow);         { arow  from mouse }
  13.  if (Button = mbRight)
  14.       AND (aCol=2)
  15.        AND (aRow=xRow)                                
  16.    then
  17.      TDBGrid(Sender).PopupMenu.Popup; {popup ONLY over the celected cell}
  18. end;
  19.  
Laz 2.0.2/FPC 3.0.4/ win10/64
Laz 2.0.0/FPC 3.0.4/ mint-mate 19.1

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: PopupMenu on a selected cell of a DBGrid
« Reply #5 on: January 08, 2019, 02:23:09 pm »
Yes, that would work too. But you don't need the double cast:
  xRow := TDummyDBGrid(TDBGrid(Sender)).Row;
you can just do:
  xRow := TDummyDBGrid(Sender).Row;

Also, although probably not needed it's always good manners to check the type before hard-casts like those:
Code: Pascal  [Select][+][-]
  1.   if Sender is TDBGrid then begin
  2.     { Now you can safely do TDBGrid(Sender).whatever }
  3.   end;
« Last Edit: January 08, 2019, 02:27:02 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018