RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TStrings.Count Property

Introduces an abstract property to represent the number of strings in the list.

Pascal
property Count: Integer;
C++
__property int Count;

Descendants of TStrings implement a Count property to indicate the number of strings in the list. 

Use the Count property when iterating over all the strings in the list, or when trying to locate the position of a string relative to the last string in the list.  

C++ Examples: 

 

/*
This example calls the TSession GetAliasNames method to fill
the list box with Borland Database Engine (BDE) alias names.
*/
void __fastcall TAdhocForm::Button1Click(TObject *Sender)
{
  TStringList *MyStringList = new TStringList();
  try
  {
    Session->GetAliasNames(MyStringList);
    // fill a list box with alias names for the user to select from
    for (int I = 0; I < MyStringList->Count; I++)
      ListBox1->Items->Add(MyStringList->Strings[I]);
  }
  __finally
  {
    delete MyStringList;
  }
}
/*
The following example updates the strings in a list box
given the strings contained in another list box. If a string
in the source list box has the form Name=Value and the
destination list box contains a string with the same Name
part, the Value part in the destination list box will be
replaced by the source’s value.  TListBox objects that are
not name-value pairs are not assigned a Name and IndexOfName
will return -1.  TStrings Values are referenced using the
name AnsiString as an index.
*/
void MergeStrings(TStrings *Dest, TStrings *Source)
{
  for (int i = 0; i < Source->Count; i++)
  {
    if (Source->Strings[i].Pos("=") > 1)
    {
      int DI = Dest->IndexOfName(Source->Names[i]);
      if (DI > -1)
        Dest->Values[Source->Names[i]] = Source->Values[Source->Names[i]];
    }
  }
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  MergeStrings(ListBox1->Items,ListBox2->Items);
}

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  ListBox1->Items->Add("Plants = 10");
  ListBox1->Items->Add("Animals = 20");
  ListBox1->Items->Add("Minerals = 15");
  ListBox2->Items->Add("Animals = 4");
  ListBox2->Items->Add("Plants = 3");
  ListBox2->Items->Add("Minerals = 78");
}
/*
To use this example, place a TComboBox and TRichEdit or
TMemo on a form.  During form creation, a list of font names
is loaded into the combo box. When the combo box is clicked,
the memo or Rich Edit control font is set to the
corresponding font name in the combo box.
*/
void __fastcall TForm1::FormCreate(TObject *Sender)
{
  for (int i = 0; i < Screen->Fonts->Count; i++)
   ComboBox1->Items->Add(Screen->Fonts->Strings[i]);
//  ComboBox1->Items = Screen->Fonts;  // Another way to do it.
}

void __fastcall TForm1::ComboBox1Change(TObject *Sender)
{
  RichEdit1->Font->Name = ComboBox1->Text;
}

 

Delphi Examples: 

{
This example calls the TSession GetAliasNames method to fill
the list box with Borland Database Engine (BDE) alias names.
}
procedure TAdhocForm.Button1Click(Sender: TObject);
var
  MyStringList: TStringList;
  I: Integer;
begin
  MyStringList := TStringList.Create;
  try
    Session.GetAliasNames(MyStringList);
    { fill a list box with alias names for the user to select from }
    for I := 0 to MyStringList.Count - 1 do
      ListBox1.Items.Add(MyStringList[I]);
  finally
    MyStringList.Free;
  end;
end;
{
The following example updates the strings in a list box
given the strings contained in another list box. If a string
in the source list box has the form Name=Value and the
destination list box contains a string with the same Name
part, the Value part in the destination list box will be
replaced by the source’s value.  TListBox objects that are
not name-value pairs are not assigned a Name and IndexOfName
will return -1.
}
procedure MergeStrings(Dest, Source: TStrings);
var
  I, DI: Integer;
  begin
  for I := 0 to Source.Count - 1 do
  begin
    if Pos ('=', Source[I]) > 1 then
    begin
      DI := Dest.IndexOfName(Source.Names[I]);
      if DI > -1 then Dest[DI] := Source[I];
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  MergeStrings(ListBox1.Items, ListBox2.Items);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  ListBox1.Items.Add('Plants = 10');
  ListBox1.Items.Add('Animals = 20');
  ListBox1.Items.Add('Minerals = 15');
  ListBox2.Items.Add('Animals = 4');
  ListBox2.Items.Add('Plants = 3');
  ListBox2.Items.Add('Minerals = 78');
end;
{
To use this example, place a TComboBox and TRichEdit or
TMemo on a form.  During form creation, a list of font names
is loaded into the combo box. When the combo box is clicked,
the memo or Rich Edit control font is set to the
corresponding font name in the combo box.
}
procedure TForm1.FormCreate(Sender: TObject);
var
  i : Integer;
begin
  for i := 0 to Screen.Fonts.Count - 1 do
   ComboBox1.Items.Add(Screen.Fonts.Strings[i]);
//  ComboBox1.Items := Screen.Fonts;  // Another way to do it.
  RichEdit1.Lines.Text := 'How is it to be you?';
end;

procedure TForm1.ComboBox1Click(Sender: TObject);
begin
  RichEdit1.Font.Name := ComboBox1.Text;
end;

 

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