RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TStrings.IndexOf Method

Returns the position of a string in the list.

Pascal
function IndexOf(const S: string): Integer; virtual;
C++
virtual __fastcall int IndexOf(const AnsiString S);

Call IndexOf to obtain the position of the first occurrence of the string S, or of a string that differs from S only by case. IndexOf returns the 0-based index of the string. Thus, if S matches the first string in the list, IndexOf returns 0, if S is the second string, IndexOf returns 1, and so on. If the string is not in the string list, IndexOf returns -1.

Note: If the string appears in the list more than once, IndexOf returns the position of the first occurrence.
 

C++ Examples: 

 

/*
This example uses a file list box, a directory list box, and
a label on a form. When the user uses the directory list box
to change directories, a message appears and the color of
the form changes if the file AUTOEXEC.BAT is in the new
directory. The code is written in the OnChange event of the
directory list box:
*/
void __fastcall TForm1::DirectoryListBox1Change(TObject *Sender)
{
  FileListBox1->Directory = DirectoryListBox1->Directory;
  if (FileListBox1->Items->IndexOf("AUTOEXEC.BAT") > -1)
  {
    Color = clYellow;
    Label1->Caption = "You are in the root directory!";
  }
}

 

Delphi Examples: 

{
This example uses a file list box, a directory list box, and
a label on a form. When the user uses the directory list box
to change directories, a message appears and the color of
the form changes if the file AUTOEXEC.BAT is in the new
directory. The code is written in the OnChange event of the
directory list box:
}
procedure TForm1.DirectoryListBox1Change(Sender: TObject);
begin
  FileListBox1.Directory := DirectoryListBox1.Directory;
  if FileListBox1.Items.IndexOf('AUTOEXEC.BAT') > -1 then
  begin
    Color := clYellow;
    Label1.Caption := 'You are in the root directory!';
  end
  else
  begin
    Color := clBtnFace;
    Label1.Caption := '';
  end;
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!