2
votes

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: 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.

3
Do not limit mouse movement! Users will hate you for that and stop using your software.Toon Krijthe

3 Answers

1
votes

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

1
votes

A nice solution

Check the options property on TStringGrid There is an Option called 'goRangeSelect' Set this to false using the object inspector

Or programatically this can be done by

StringGrid1.Options - [goRangeSelect];
0
votes

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.