Single and multiple selection detection in Delphi TStringGrid

This is my next previous Multi-Select Delphi TStringGrid question defining selected rows relative to String Grids in Delphi. This is another question.

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

I noticed that the properties of TStringGrid.Selection.Top, Bottom are not necessarily accurate (inside the event itself). Basically, if someone moves from selecting multiple rows to one row, the properties are selections. * Not updated, whereas if you select multiple lines, they do it.

The ARow parameter is 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 is selected.

For example, if only one row is selected, then use the Arow parameter, if more than several rows use the Selection properties. * to determine which rows are currently selected.

There should be an easier way ...

Thank!

+5
source share
4 answers

I think part of the problem is related to terminology. Until you understand what is going on, it should be difficult to enter that “choice is used to mean“ emphasis ”and“ focus. ”In this particular case, there should be a difference between the two.

, , , ( ) , .

OnSelectCell . , . , . CanSelect ( , , CanFocus, , .. , , OnSelectCell).

goRangeSelect TDrawGrid.Selection, , . () , .

, . Selection , ACol ARow, . Selection , , . , , Selection , . ( ), Selection ( , reset CanSelect ,).

, OnSelectCell Selection . @Sam OnMouseUp *. : , , " ". , , OnMouseMove , , " ".

OnDrawCell , .


* , , OnKeyUp, .

+4
for RowIndex := StringGrid1.Selection.Top to StringGrid1.Selection.Bottom do
begin
  DoSomethingWithRow(RowIndex);
end;

, . , Selection OnSelectCell, (.. select). , stringGrid OnMouseUp. , .

0

, OnDrawCell onSelectCell, , , , .

, . :

  • TStringGrid.Selection OnDrawCell.
  • TStringGrid.Selection OnSelectCell IFF .

public
  previousHighlightCount : integer; //flag to ensure that the necessary code within the onDraw only gets called once per row selection(s).  Initialize to '1' in onFormCreate.


procedure Grid.OnDrawCell(...)
begin
...
SelectionCount := Grid.Bottom - Grid.Top;**
if ((SelectionCount = 1) AND (previousHighlightCount  1)) then  
begin                                                                               
   GridUpdateEdits;  //your routine to update the grid properly for one row.*    
   previousHighlightCount := 1;
end
else
  previousHighlightCount := PrtEdtGrid.SelectionCount;   //the routine for multiply selected rows is in the onSelectCell Event and onSeelctCell works for multiple selections.
....
end;


, !!

0

StringGrid1.Selection.Top to StringGrid1.Selection.Bottom is what worked fine for me since I am using the onkeypress event to select / deselect.

0
source

All Articles