Delphi – Detecting single vs multiple selections in Delphi TStringGrid

delphiselectselectiontstringgrid

This is a follow up to my previous question Delphi TStringGrid multi select, determining selected rows regarding Delphi String Grids. It's a different question.

I was looking more closely at the ONSelectCell Event
TSelectCellEvent = procedure (Sender: TObject; ACol, ARow: Longint; var CanSelect: Boolean) of object;

I noticed that the TStringGrid.Selection.Top,Bottom properties are not necessarily accurate (within the event itself). Basically, if someone goes from selecting multiple rows to just one row, the selection.* properties do not get updated, whereas if one selects multiple rows, they do.

The ARow parameter does get updated regardless of whether one or more rows are selected, but this will only help me if I can determine that one and only one row was selected.

Eg, If it's just one row that was selected, then use Arow parameter, if more than one row then use Selection.* properties to determine which row(s) are currently selected.

There must be an easier way….

Thank you!

Best Answer

I think, part of the problem is in terminology used. Until you completely understand what is happening, it must be confusing to find how ‘select’ is used to mean both ‘highlight’ and ‘focus’. In this particular case there should be distinction between the two.

Before I proceed, I'd like you to keep in mind that the focused cell can also be (and actually is) highlighted, but a highlighted cell is not necessarily the focused one.

Now, the OnSelectCell event has to do with focusing. The handler is fired when the cell is clicked or when you are trying to navigate over it with navigation keys. In short, the handler is invoked when there's an attempt to focus a cell. You can prohibit focusing the cell by resetting the CanSelect parameter (which, again, means essentially CanFocus, because the cell can be selected, i.e. highlighted, without being focused, and you can't control that with OnSelectCell).

The goRangeSelect option and TDrawGrid.Selection property, on the other hand, have to do with selecting as highlighting. The former allows you (the user) to highlight more than one cell, while the latter points to the range of those cells highlighted.

Now to my main point. Upon invoking the handler in question, Selection is never accurate, i.e. it is not correlated with the ACol & ARow parameters that are passed to the handler. Selection contains the range of cells that were highlighted just before calling the handler, and it never changes by itself within the handler. Whether one cell or more than one, Selection stays the same until the handler exits. And it is when that happens (the handler exits) that Selection changes (and the result depends on whether you reset CanSelect or not, by the way).

So, in conclusion, you cannot use OnSelectCell to determine the actual Selection as the result of the user's most recent action. Instead I would suggest following @Sam's advice and use the OnMouseUp* event. It also allows you to have control over selection: you can correct the final range if you think the user has selected ‘too much’. In the latter case I would probably consider using OnMouseMove instead, though, as it allows you to respond more smoothly by correcting the range ‘on the fly’.

OnDrawCell seems fine too as long as you need just to determine the selection.


*Following your comment, I must add, that you'd also have to employ OnKeyUp as well, to handle selections made with the keyboard.

Related Topic