JO SeongGng
JO SeongGng

Reputation: 567

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

I try to select or click a cell externally.

When I use tStringGrid.Col and tStringGrid.Row to select a cell, onSelectCell event runs twice.

How can I make it to be processed once?

If I use tStringGridSelectCell event to avoid the problem, a selection rectangle doesn't move to the position.

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;

Upvotes: 0

Views: 647

Answers (2)

Tom Brunberg
Tom Brunberg

Reputation: 21033

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

Temporarily disable the 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;

Upvotes: 2

Sir Rufo
Sir Rufo

Reputation: 19096

You can set the selected cell/cells with TStringGrid.Selection property.

Upvotes: 1

Related Questions