0

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;
JO SeongGng
  • 567
  • 3
  • 18

2 Answers2

2

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;
Tom Brunberg
  • 20,312
  • 8
  • 37
  • 54
  • i would rather look for activating cell after a selection. thanks. – JO SeongGng Dec 03 '15 at 07:43
  • @JO like setting focus to the grid? I added it to the code. – Tom Brunberg Dec 03 '15 at 07:50
  • @JO Or, if you meant something else, please define 'Activate'. – Tom Brunberg Dec 03 '15 at 07:56
  • @JO **You** need to tell me what you mean with 'activating' the cell. – Tom Brunberg Dec 03 '15 at 08:20
  • no. setfocus doesn't activate a cell or cells. i used 'activate' to explain a cell which is selected and seen as if it is clicked. the above code activates it but when a selected cell or cells are not in visible range, selection codes don't make a cell or cells to be seen. thanks. – JO SeongGng Dec 03 '15 at 08:24
  • @JOSeongGng, use [TopRow](http://docwiki.embarcadero.com/Libraries/Seattle/en/Vcl.Grids.TCustomDrawGrid.TopRow) to set the upper visible row in the grid. – LU RD Dec 03 '15 at 08:38
  • @JOSeongGng, you can check if the row is visible before setting the `TopRow`. Use the [VisibleRowCount](http://docwiki.embarcadero.com/Libraries/Seattle/en/Vcl.Grids.TCustomGrid.VisibleRowCount) property in conjunction with `TopRow`. – LU RD Dec 03 '15 at 08:52
  • @JO FYI, in my test with all options and other properties unchanged except `ColCount` and `RowCount` both set to 15. Further the button event as above in my answer. Scrolling first to cell 15, 15 and then clicking the button, cell 2,2 becomes selected and is automatically scrolled in view. Tested in Delphi XE7 and Delphi 7. – Tom Brunberg Dec 03 '15 at 09:33
1

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

Sir Rufo
  • 18,395
  • 2
  • 39
  • 73