tags:

views:

115

answers:

2

Hi.
I have a heavily modified control based on TStringGrid. I want to allow the user to make selections in this grid but restrict the selection one single row (the current row).

Implementation: When the user presses the left mouse button I am using a dirty hack: On OnMouseDown event I capture the mouse cursor and keep it on the current row. The user can move the mouse device up and down on its pad but the cursor will not go up or down. It will stay on the current row. When the user releases the button (OnMouseUp event), I release the capture.

However, this hack is as I said very dirty. There are several problems. For example, if the user presses the left mouse button (LMB) and then without releasing that button it presses the right button, the associated pop-up menu will pop but the mouse capture will never be released. So, the mouse will be locked in a screen region until the user will has the brilliant idea to click the LMB one more time to unlock the mouse. This may be a bug in D7. There is a separate post about it here: http://stackoverflow.com/questions/3285604/tstringgrid-onmouseup-is-not-called

There is a elegant way to do this?


Edit: OnSelectCell is not working. OnSelectCell event is called only once when you click the cell. If you keep the button pressed and move the mouse to expand the selection, OnSelectCell will not be called again. Indeed MoveCurrent appears only in TCustomGrid.MouseDown.

+1  A: 

Use the OnSelectCell event and set the CanSelect var parameter depending on whether the ARow parameter is what you want.

joe snyder
It is not working. The OnSelectCell is called only once when you click the cell. If you keep the button pressed and move the mouse to expand the selection, OnSelectCell will not be called again.
Altar
@Altar, I believe this strange behavior is caused by the `goRangeSelect` in the grid's `Options`. Since you're only allowing a single row to be selectable range selection would not make much sense. Give it a try with the option turned off...
Sertac Akyuz
A: 

This answer is not elegant at all, but it works.

My solution is NOT to use the PopupMenu property of the StringGrid. Instead I implemented my own PopUpMenu property. Works almost perfect. There is on small problem, the bottom of the pop-up menu appears next to the cursor and not its top.

Altar