Board index » delphi » String Grid and Right Mouse Click

String Grid and Right Mouse Click

I am trying to select a cell using the right mouse button but having
problems.  Here's the details:

I have a string grid with goRowSelect set to true so the whole line will be
selected.  When I click a row with the left mouse button the row gets
selected as it should.  However I would like to be able to select a row
using the right mouse click.  When the user does a right mouse click on a
particular row a popup menu appears with a few actions to perform on that
row.  The problem is a right click will not select a row.  For example if
the focus is on row 1 and I right click on row 3 then select the delete
action from my popup menu row 1 gets deleted because row 3 never gets the
focus.

Does someone know the solution to this problem?

Thanks,
Jason

 

Re:String Grid and Right Mouse Click


Jason, here is some very scratchy code for you:

procedure TForm1.StringGrid1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var row, col: integer;
begin
  stringgrid1.MouseToCell( X,Y, col, row );
  stringgrid1.Row := row;
  if button = mbRight then popupmenu1.Popup(x,y);
end;

The only thing you have to change is the coordinates passed to the Popup
method of the popup menu. There is a property/method something like
ClientToScreen, perhaps of the string grid. Use this to convert (X,Y), which
are relative to the string grid, to coordinates relative to the screen.

Any further questions, email me or reply to the group.

Regards,

Colin.

Other Threads