views:

143

answers:

3

I'm having a problem with using a TStringGrid and Popup menu

I want to know the Row / Column of the cell that was last active when select an item from my Popup menu. However when I click on the popup menu, the StringGrid.Row is returned as -1.

I've tried using MouseToCell as part of OnClick, but even after setting SG.Row it still returns as -1 in the PopUp menus routines... I suspect that the problem is the Grid losing the focus.

Are there any solutions to this that don't require OnClick setting a global variable?

I'm using an Action List linked to the items on the Popup Menu to make sure that the actions are consistent between the toolbar and the Popup Menu

+2  A: 

I am afraid that I do not fully understand what you mean. When I left-click a cell in a string grid, it gets selected, but not when I right-click it. When I right-click it, the popup menu is shown (if assigned), and on MenuItemClick I can easily read the row and col currently selected. See example video.

I guess that you actually want this: you want right-clicks to change the active cell as well as left-clicks. This is easily done:

procedure TForm1.StringGrid1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbRight then
    StringGrid1.Perform(WM_LBUTTONDOWN, 0, MakeLParam(Word(X), Word(Y)));
end;
Andreas Rejbrand
I don't think you really have to check whether the buttons are swapped. When the buttons are swapped, pressing the left button generates right-button messages and vice versa. When the buttons are swapped and the left button is pressed, the `Button` argument will really be `mbRight` already.
Rob Kennedy
@Rob Kennedy: Yes, I figured that out while sleeping...
Andreas Rejbrand
If I try and get the above to work then Left Click stops working, and I get a stack overflow!Without the above I click on a cell and the "active border" moves to the selected cell. If I then right click and select a Menu Item the value reported as StringGrid.Col is -1
Dan Kelly
@Dan Kelly: Does this happen to you when using a `TStringGrid` with its default settings?
Andreas Rejbrand
A: 

In one of my TStringGrid-based controls I am using MouseDown/MouseUp event to handle that pop-up menu because I have two different contextual menus, depending on which area of TStringGrid you've clicked. Works like a charm. Just make sure you call inherited BEFORE your code.

--
Please note that there is something strange about the order in which the events are called when you pop-up the contextual menu. More exactly, when you press the RMB and the pop-up menu pops the MouseUp event is not called immediately. It is called next time you press a mouse button (any button).

See this also: http://stackoverflow.com/questions/3285604/tstringgrid-onmouseup-is-not-called

Altar
A: 

Hmm... I'm unable to duplicate the problem in my D2010.

A quick thought is that perhaps the problem occurs because you did not have any rows selected? Would presetting the StringGrid's Row to, say, 0 first in your Form's OnCreate help?

Chee Meng