0

I use windows 10 and seattle.

I try to change tStringGrid.RowCount without running onSelectCell event because there is a something which should not be run when a cell is not clicked or selected.

Sometimes changing tStringGrid.RowCount fires tStringGrid onSelectCell event. After implementing the following code with default tStringGrid, click the form -> click the button -> click any cell whose row index is bigger than 0 -> click the form again then onSelectCell event fires on the last clicking the form event.

I want to know whether this is a bug or I misunderstand something. In the former case I need bypass that and I can and in the latter case please let me know the reason to solve the problem.

procedure TForm1.Button1Click(Sender: TObject);
begin
  StringGrid1.RowCount := 5;
end;

procedure TForm1.FormClick(Sender: TObject);
begin
  StringGrid1.RowCount := 1; //  at the second time this fires tStringGrid.onSelectCell Event
end;

procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
begin
  Memo1.Lines.Add(IntToStr(ACol) + ' ' + IntToStr(ARow));
end;
JO SeongGng
  • 567
  • 3
  • 18
  • 2
    This makes perfect sense, and is no way a bug. It's a flaw in your understanding of what's happening. :-) The button click increases the rowcount, and the selection becomes one of the new rows. You then click back on the form, which removes all of the rows except 1; if the selection is > 0, it has to go somewhere, and that generates an OnSelectCell event. If you don't want that to happen, disable the OnSelectCell event before changing RowCount. – Ken White Feb 01 '16 at 13:35
  • When you reduce the number of rows you can remove the selected cell. If you don't want to event to fire, disable it temporarily. – David Heffernan Feb 01 '16 at 13:35
  • @KenWhite If button click makes the selection become why the first button click doesn't make it fire? Without button click, just cell click -> form click doesn't fire onSelectCell event. In this case absolutely a cell is selected but reducing row count doesn't fire the event. – JO SeongGng Feb 01 '16 at 13:44

1 Answers1

1

The behaviour that you report is natural. When you reduce the number of rows, if you are removing the row containing the selected cell, then a new cell has to be selected. The logic here is that a cell in the last remaining row is selected, and the selected column is not modified. Since a new cell is selected, the OnSelectCell event is fired.

This is not a bug. The behaviour is sensible, and as designed.

If you wish to suppress the OnSelectCell event while you perform certain actions, disable it temporarily.

StringGrid1.OnSelectCell := nil;
try
  // do stuff that might change the selection
finally
  StringGrid1.OnSelectCell := StringGrid1SelectCell;
end;
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490