RAD Studio
ContentsIndex
PreviousUpNext
Iterating Through Strings in a List

This VCL application first creates a list of strings. Then it iterates through the strings, changing all string characters to uppercase. It consists of the following steps:

  1. Create a VCL Form with Buttons and TListBox controls.
  2. Write the code to create a string list and add strings to it.
  3. Write the code to iterate through the string list to process string characters.
  4. Run the application.

To create a VCL Form with TButton and TListBox controls

  1. Choose FileNewOtherDelphi Projects or C++Builder Projects and double-click the VCL Forms Application icon. The VCL Forms Designer is displayed.
  2. From the Standard page of the Tool palette, place two TButtons and a TListBox component on the form.
  3. Select Button1 on the form.
  4. In the Object Inspector, enter Add for the Name and Caption properties.
  5. Select Button2 on the form.
  6. In the Object Inspector, enter ToUpper for the Name and Caption properties.

To create a string list and add strings to it

  1. Select the Add button on the form.
  2. In the Object Inspector, double-click the OnClick action on the Events tab. The Code Editor displays, with the cursor in the TForm1.AddClick (Delphi) or TForm1::AddClick (C++) event handler block.
  3. For Delphi, place the cursor before the begin reserved word; then press ENTER. This creates a new line above the code block.
  4. For Delphi, insert the cursor on the new line created, and type the following variable declaration:

var 
  MyList: TStringList;

  1. Insert the cursor within the code block, and type the following code:

MyList := TStringList.Create;
    try
      with MyList do
      begin
        Add('Mice');
        Add('Goats');
        Add('Elephants');
        Add('Birds');
        ListBox1.Items.AddStrings(MyList);
      end;
    finally
      MyList.Free;
    end;
TStringList *MyList = new TStringList();
try {
 MyList->Add(“Mice”);
 MyList->Add(“Goats”);
 MyList->Add(“Elephants”);
 MyList->Add(“Birds”);
 ListBox1–>Items->AddStrings( MyList );
} __finally {
 MyList->Free();
}
To change all characters to uppercase

  1. Select the ToUpper button on the form.
  2. In the Object Inspector, double-click the OnClick action on the Events tab. The Code Editor displays, with the cursor in the TForm1.ToUpperClick (Delphi) or TForm1::ToUpperClick (C++) event handler block.
  3. For Delphi, place the cursor before the begin reserved word; then press return. This creates a new line above the code block.
  4. For Delphi, insert the cursor on the new line created and type the following variable declaration:

var 
 Index: Integer;

  1. Insert the cursor within the code block, and type the following code:

for Index := 0 to ListBox1.Items.Count - 1 do
    ListBox1.Items[Index] := UpperCase(ListBox1.Items[Index]);
for( int i = 0; i < ListBox1–>Items->Count; i++ ) {
 ListBox1–>Items[i] = UpperCase( ListBox1–>Items[i] );
}
To run the application

  1. Save your project files; then choose RunRun to build and run the application. The form displays with the controls.
  2. Click the Add button. The strings 'Mice', 'Goats', 'Elephants', and 'Birds' display in the order listed.
  3. Click the ToUpper button. The string characters display in uppercase.

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