How to prohibit an unintended tStringGrid onSelectCell event firing
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;
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;
链接地址: http://www.djcxy.com/p/50640.html