如何避免重复执行 tStringGrid.Col & tStringGrid.Row

How to avoid a duplicate implementation of tStringGrid.Col & tStringGrid.Row

我尝试 select 或单击外部单元格。

当我使用 tStringGrid.Col 和 tStringGrid.Row 到 select 一个单元格时,onSelectCell 事件运行两次。

如何让它被处理一次?

如果我使用 tStringGridSelectCell 事件来避免这个问题,selection 矩形不会移动到该位置。

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    procedure FormClick(Sender: TObject);
    procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
  private
    TestCount: Integer;
  end;

procedure TForm1.FormClick(Sender: TObject);
var
  _Boolean: Boolean;
begin
  StringGrid1.Col := 2;
  StringGrid1.Row := 2;

  // StringGrid1SelectCell(Self, 2, 2, _Boolean); // the event runs once well but the cell is not visible when the position is out of sight.
end;

procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
begin
  // something to be implemented

  Inc(TestCount);

  Caption := IntToStr(TestCount); // testcount is 2
end;

您可以将所选的cell/cells设置为TStringGrid.Selection属性。

How can I make it (OnSelectCell) to be processed once?

暂时禁用 TSelectCellEvent

procedure TForm16.Button1Click(Sender: TObject);
var
  temp: TSelectCellEvent;
begin
  temp := StringGrid1.OnSelectCell;
  StringGrid1.OnSelectCell := nil;
  StringGrid1.Col := 2;
  StringGrid1.OnSelectCell := temp;
  StringGrid1.Row := 2;
  StringGrid1.SetFocus; // Optional, can be useful if goEditing is set
end;