delphitstringgrid

Delphi Compiler expect [ on index of call of TStringsGrid cols(TStrings) property


Dear Stack overflow Community

I just resently started to learn delphi. I tried to get the Index of a Colum in a TString Grid over the display name. For that i tried to call the indexof method on the cols property of the grid but the Compiler give me out this

[dcc32 Fehler] Unit2.pas(30): E2029 '[' erwartet, aber '.' gefunden 

as he would only allow indexed calls but that would litteraly beat the sense of Indexof witch is made vor the case the index is maybe each time different or could change in the future. It would be nice if somebody could point out where i went wrong. thanks in advance

here the full code

unit Unit2;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Grids;

type
  TForm2 = class(TForm)
    StringGrid1: TStringGrid;
    Button1: TButton;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
begin
    StringGrid1.Cols.IndexOf('Test');
end;

end.

Solution

  • In case you want to find the index of the column with the header text 'Test', you can do so with Rows[0].IndexOf('Test'), assuming the header is stored in row 0.