Kristian Sander
Kristian Sander

Reputation: 291

Focus on an arbitrary row / column in a TStringGrid

I run Delphi 7 Enterprise on an XP SP3 machine.

I can set focus on a TEdit by using: TEdit.SetFocus;

And I can set focus on a TStringGrid by using: TStringGrid.SetFocus;

But, this is only part of what I want.

How do I set focus on a certain cell (Column/Row) in a TStringGrid?

TStringGrid.SetFocus(Col,Row)

Upvotes: 1

Views: 1160

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 595792

You can use the grid's Col and Row properties:

Specifies the index of the column that contains the selected cell.

Specifies the index of the row that contains the selected cell.

StringGrid1.SetFocus;
StringGrid1.Col := Col;
StringGrid1.Row := Row;

Which internally just calls the grid's protected FocusCell() method, which you can call directly by using an accessor class:

type
  TStringGridAccess = class(TStringGrid)
  end;

StringGrid1.SetFocus;
TStringGridAccess(StringGrid1).FocusCell(Col, Row, True);

Upvotes: 2

Tom Brunberg
Tom Brunberg

Reputation: 21033

You define a TGridRect for the cell to focus on (select), alternatively range of cells to focus on (if you have goRangeSelect = True in Options.

Then you select that TGridRect.

Example:

procedure TForm30.Button1Click(Sender: TObject);
var
 AGridRect: TGridRect;
begin
  AGridRect.Left := 2;
  AGridRect.Top  := 2;
  AGridRect.Right := 2;
  AGridRect.Bottom := 2;
  StringGrid1.Selection := AGridRect;
end;

Upvotes: 2

Related Questions