RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TStrings.Add Method

Adds a string at the end of the list.

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

Call Add to add a string to the end of the list. Add returns the index of the new string.  

C++ Examples: 

 

/*
This example adds text to a TRichEdit and to a StatusBar.
This example requires no additional controls. All controls are
created dynamically within the form's OnCreate event handler.
Note: Be sure to add ComCtrls to your uses clause to support 
TRichEdit.
*/

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  TStatusBar *status = new TStatusBar(this); // The owner will clean this up.
  // Display the StatusBar control on the surface
  // of the form.
  status->Parent = this;
  // Allow a single line of text -- no panels.
  status->SimplePanel = true;
  // Provide sample text.
  // Create a new instance of TRichEdit, set its
  // properties, and display on the form.
  TRichEdit *rich = new TRichEdit(this);  // The owner will clean this up.
  // Display the RichEdit control on the surface
  // of the form.
  rich->Parent = this;
  // The RichText control takes all space left
  // over from the StatusBar.
  rich->Align = alClient;
  // Add 50 lines to the RichText
  for (int i = 1; i <= 50; i++)
    rich->Lines->Add(AnsiString("This is line ") + IntToStr(i));
  rich->SelStart = 0; // set text cursor to first character
  status->SimpleText = IntToStr(rich->Lines->Count) + " Lines loaded";
}
/*
This example uses a tab control to display the contents of
several files. To run the example, place a tab control on a
form and add a memo control that fits into its client area.
Be sure to leave enough room for the tabs when they appear.
Then add an OpenDialog and a button to the form.  The
"Add a file" button adds a single file to the tabcontrol.
The "Assign files" button removes previous files and can be
used to assign multiple files.  To assign multiple files,
use CNTL Select or SHIFT Select to select files in the
OpenDialog.
*/
void __fastcall TForm1::Add_a_fileClick(TObject *Sender)
{
  OpenDialog1->Options << ofAllowMultiSelect << ofFileMustExist << ofHideReadOnly;
  if (OpenDialog1->Execute())
  {
    int index = TabControl1->Tabs->Add(OpenDialog1->FileName);
    Memo1->Lines->LoadFromFile(TabControl1->Tabs->Strings[index]);
    TabControl1Change(Sender);
  }
}

void __fastcall TForm1::Assign_filesClick(TObject *Sender)
{
  OpenDialog1->Options << ofAllowMultiSelect << ofFileMustExist << ofHideReadOnly;
  if (OpenDialog1->Execute())
  {
    TabControl1->Tabs->Assign(OpenDialog1->Files);
    Memo1->Lines->LoadFromFile(TabControl1->Tabs->Strings[TabControl1->TabIndex]);
  }
}
/*
Place the following code in the tab control’s OnChange event
handler:
*/
void __fastcall TForm1::TabControl1Change(TObject *Sender)
{
    Memo1->Lines->LoadFromFile(
      TabControl1->Tabs->Strings[TabControl1->TabIndex]);
}

 

Delphi Examples: 

{
This example adds text to a TRichEdit and to a StatusBar.
This example requires no additional controls. All controls are
created dynamically within the form's OnCreate event handler.
Note: Be sure to add ComCtrls to your uses clause to support 
TRichEdit.
}
procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  with TStatusBar.Create(Self) do
  begin
    // Display the StatusBar control on the surface
    // of the form.
    Parent := Self;
    // Allow a single line of text -- no panels.
    SimplePanel := True;
    // Provide sample text.
    // Create a new instance of TRichEdit, set its
    // properties, and display on the form.
    with TRichEdit.Create(Self) do
    begin
      // Display the RichEdit control on the surface
      // of the form.
      Parent := Self;
      // The RichText control takes all space left
      // over from the StatusBar.
      Align := alClient;
      // Add 50 lines to the RichText
      for i := 1 to 50 do
        Lines.Add('Adding line ' + IntToStr(i));
      SelStart := 0; // set text cursor to first character
      SimpleText := 'Number of Lines loaded to RichText: ' +
        IntToStr(Lines.Count);
    end;
  end;
end;
{
This example uses a tab control to display the contents of
several files. To run the example, place a tab control on a
form and add a memo control that fits into its client area.
Be sure to leave enough room for the tabs when they appear.
Then add an OpenDialog and a button to the form.  The
"Add a file" button adds a single file to the tabcontrol.
The "Assign files" button removes previous files and can be
used to assign multiple files.  To assign multiple files,
use CNTL Select or SHIFT Select to select files in the
OpenDialog.
}
procedure TForm1.Add_a_fileClick(Sender: TObject);
var index : integer;
begin
  OpenDialog1.Options :=
    [ofAllowMultiSelect, ofFileMustExist, ofHideReadOnly];
  if OpenDialog1.Execute then
  begin
    index:= TabControl1.Tabs.Add(OpenDialog1.FileName);
    Memo1.Lines.LoadFromFile(TabControl1.Tabs[index]);
    TabControl1Change(Sender);
  end;
end;

procedure TForm1.Assign_filesClick(Sender: TObject);
var index : integer;
begin
  OpenDialog1.Options :=
    [ofAllowMultiSelect, ofFileMustExist, ofHideReadOnly];
  if OpenDialog1.Execute then
  begin
    TabControl1.Tabs.Assign(OpenDialog1.Files);
    Memo1.Lines.LoadFromFile(
      TabControl1.Tabs[TabControl1.TabIndex]);
  end;
end;

{
Place the following code in the tab control’s OnChange event
handler:
}
procedure TForm1.TabControl1Change(Sender: TObject);
begin
  with TabControl1 do
    Memo1.Lines.LoadFromFile(Tabs[TabIndex]);
end;

 

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