如何禁止意外的 tStringGrid onSelectCell 事件触发
How to prohibit an unintended tStringGrid onSelectCell event firing
我用windows10和西雅图。
我尝试在没有 运行ning onSelectCell 事件的情况下更改 tStringGrid.RowCount,因为当没有单击或选择单元格时,有些东西不应该 运行。
有时更改 tStringGrid.RowCount 会触发 tStringGrid onSelectCell 事件。使用默认的 tStringGrid 实现以下代码后,单击表单 -> 单击按钮 -> 单击行索引大于 0 的任何单元格 -> 再次单击表单然后 onSelectCell 事件在最后一次单击表单事件时触发。
我想知道这是一个错误还是我误解了什么。在前一种情况下,我需要绕过它,我可以,在后一种情况下,请告诉我解决问题的原因。
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;
您报告的行为是自然的。减少行数时,如果要删除包含所选单元格的行,则必须选择一个新单元格。这里的逻辑是选择最后剩余行中的一个单元格,并且不修改所选列。由于选择了一个新单元格,因此触发了 OnSelectCell
事件。
这不是错误。这种行为是明智的,并且符合设计。
如果您希望在执行某些操作时抑制 OnSelectCell
事件,请暂时禁用它。
StringGrid1.OnSelectCell := nil;
try
// do stuff that might change the selection
finally
StringGrid1.OnSelectCell := StringGrid1SelectCell;
end;
我用windows10和西雅图。
我尝试在没有 运行ning onSelectCell 事件的情况下更改 tStringGrid.RowCount,因为当没有单击或选择单元格时,有些东西不应该 运行。
有时更改 tStringGrid.RowCount 会触发 tStringGrid onSelectCell 事件。使用默认的 tStringGrid 实现以下代码后,单击表单 -> 单击按钮 -> 单击行索引大于 0 的任何单元格 -> 再次单击表单然后 onSelectCell 事件在最后一次单击表单事件时触发。
我想知道这是一个错误还是我误解了什么。在前一种情况下,我需要绕过它,我可以,在后一种情况下,请告诉我解决问题的原因。
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;
您报告的行为是自然的。减少行数时,如果要删除包含所选单元格的行,则必须选择一个新单元格。这里的逻辑是选择最后剩余行中的一个单元格,并且不修改所选列。由于选择了一个新单元格,因此触发了 OnSelectCell
事件。
这不是错误。这种行为是明智的,并且符合设计。
如果您希望在执行某些操作时抑制 OnSelectCell
事件,请暂时禁用它。
StringGrid1.OnSelectCell := nil;
try
// do stuff that might change the selection
finally
StringGrid1.OnSelectCell := StringGrid1SelectCell;
end;