如何避免重复实现tStringGrid.Col& tStringGrid.Row

时间:2015-12-03 05:36:51

标签: delphi tstringgrid

我尝试从外部选择或单击一个单元格。

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

如何让它进行一次处理?

如果我使用tStringGridSelectCell事件来避免此问题,则选择矩形不会移动到该位置。

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;

2 个答案:

答案 0 :(得分:2)

  

如何让它(OnSelectCell)进行一次处理?

暂时禁用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;

答案 1 :(得分:1)

您可以使用TStringGrid.Selection属性设置所选的单元格/单元格。