RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TIndexFiles.Add Method

Adds a new string to the list.

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

Call Add to add the string S to the list. If the list is sorted, S is added to the appropriate position in the sort order. If the list is not sorted, S is added to the end of the list. Add returns the position of the item in the list, where the first item in the list has a value of 0.

Note: For sorted lists, Add will raise an EListError exception if the string S already appears in the list and Duplicates is set to dupError. If Duplicates is set to dupIgnore, trying to add a duplicate string causes Add to return the index of the existing entry.
 

C++ Examples: 

 

/*
This example uses a list box and a label on a form. When the
application runs, a string list object is created and three
strings are added to it. The Find method searches the 
strings to look for a match with the string ‘Flowers’. If
the string is found, all the strings in the string list are
added to the list box, and the index value of the ‘Flowers’
string appears in the caption of the label control.
*/

#include <memory>       //for STL auto_ptr class

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  int Index;
  std::auto_ptr<TStringList> MyList(new TStringList());
  MyList->Add("Animals");
  MyList->Add("Flowers");
  MyList->Add("Ocean");
  MyList->Sort(); // Find will only work on sorted lists
  if (MyList->Find("Flowers", Index))
  {
    ListBox1->Items->AddStrings(MyList.get());
    Label1->Caption = "Flowers has an index value of " + AnsiString(Index);
  }
}
/*
This example creates a stringlist, adds elements to it and
assigns the strings to items of a combo box.
*/

#include <memory>       //for STL auto_ptr class

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  std::auto_ptr<TStrings> StringList(new TStringList());
  StringList->Add("This example uses A string List. ");
  StringList->Add("It is the easiest way to add strings");
  StringList->Add("to a combobox's list of strings. ");
  StringList->Add("Always remember TStrings is abstract,");
  StringList->Add("So create a TStringList instead.");
  ComboBox1->Width = 210;
  ComboBox1->Items->Assign(StringList.get());
  ComboBox1->ItemIndex = 0;
}

 

Delphi Examples: 

{
This example uses a list box and a label on a form. When the
application runs, a string list object is created and three
strings are added to it. The Find method searches the 
strings to look for a match with the string ‘Flowers’. If
the string is found, all the strings in the string list are
added to the list box, and the index value of the ‘Flowers’
string appears in the caption of the label control.
} 
procedure TForm1.FormCreate(Sender: TObject);
var
  MyList: TStringList;
  Index: Integer;
begin
  MyList := TStringList.Create;
  try
    MyList.Add('Animals');
    MyList.Add('Flowers');
    MyList.Add('Cars');
    MyList.Sort;   { Find will only work on sorted lists! }
    if MyList.Find('Flowers', Index) then
    begin
      ListBox1.Items.AddStrings(MyList);
      Label1.Caption := 'Flowers has an index value of ' + IntToStr(Index);
    end;
  finally
    MyList.Free;
  end;
end; 

 

{
This example creates a stringlist, adds elements to it and
assigns the strings to items of a combo box.
}
procedure TForm1.Button1Click(Sender: TObject);
var
  StringList: TStringList;
begin
  StringList := TStringList.Create;
  try
    with StringList do begin
      Add('This example uses A string List.');
      Add('It is the easiest way to add strings');
      Add('to a combobox''s list of strings.');
      Add('Always remember TStrings.Create method');
      Add('is abstract; So use TStringList.Create');
      Add('method instead.');
    end;
    with ComboBox1 do begin
      Width := 210;
      Items.Assign(StringList);
      ItemIndex := 0;
    end;
  finally
    StringList.free;
  end;
end;

 

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