Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2010-06

Popup menu on a TStringGrid row

4 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Juha Manninen

Hi

A popup menu has an item to delete a row from a StringGrid. This actually works, deleting the selected row:

  Grid.DeleteColRow(False, Grid.Row);

The problem is that mouse right-click didn't select the row (or cell actually) under the mouse pointer. The selected row can be somewhere far from the mouse pointer and thus deleting it is not what user expects.

I remember I had to do something complicated with VirtualTreeView on Delphi in a similar situation.

This seems like a common situation. Is there any easy solution?

Juha

Reply by email

Graeme Geldenhuys
to Juha Manninen
Juha Manninen wrote
The problem is that mouse right-click didn't select the row (or cell actually) under the mouse pointer. The selected row can be somewhere far from the mouse pointer and thus deleting it is not what user expects.

I vaguely remember seeing some "follow focus" option in the stringgrid which I think solves this problem.

Alternatively, in the OnClick or OnShowMenu (if there is such an event) you should be able to query the row under the mouse cursor position. Then set the focus row to that row, then wait to see what the user selects in the popup menu.

I remember I had to do something complicated with VirtualTreeView on Delphi

VirtualTree is over-engineering at its best! I don't like that Regards,
- Graeme -

_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/

Reply by email

Henry Vermaak
to Juha Manninen: Hi A popup menu has an item to delete a row from a StringGrid. This actually works,…
Juha Manninen wrote 5 lines
Hi A popup menu has an item to delete a row from a StringGrid. This actually works, deleting the selected row: ?Grid.DeleteColRow(False, Grid.Row);

I've done it like this (inside the popup item click):

procedure TForm1.MenuItem1Click(Sender: TObject);
var
  ControlCoord: TPoint;
begin
  ControlCoord := SG.ScreenToControl(PopupMenu1.PopupPoint);
  SG.DeleteColRow(False, SG.MouseToCell(ControlCoord).Y);
end;

Is this what you meant?

Henry

Reply by email

Juha Manninen
to Juha Manninen: Hi A popup menu has an item to delete a row from a StringGrid. This actually works,…

Thanks Graeme and Henry!

Based on your hints and code I finally created these:

-------------

procedure TReplaceNamesForm.PopupMenu1Popup(Sender: TObject);
var
  ControlCoord, NewCell: TPoint;
begin
  ControlCoord := Grid.ScreenToControl(PopupMenu1.PopupPoint);
  NewCell:=Grid.MouseToCell(ControlCoord);
  Grid.Col:=NewCell.X;
  Grid.Row:=NewCell.Y;
end;
procedure TReplaceNamesForm.InsertRow1Click(Sender: TObject);
begin
  Grid.InsertColRow(False, Grid.Row);
end;
procedure TReplaceNamesForm.DeleteRow1Click(Sender: TObject);
begin
  Grid.DeleteColRow(False, Grid.Row);
end;

----------------

Popup handler set focus to the cell under mouse and then 2 handlers Insert and Delete rows.
Now there is visual feedback when the user clicks a cell. It gets colored and there is no guessing about which row will be deleted. It is pretty much perfect now. :-)

Juha
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20100609/6d874dba/attachment.html>

Reply by email