Iterating Through Strings in a List
Go Up to How To Build VCL Forms Applications
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:
- Create a VCL Form with Buttons and TListBox controls.
- Write the code to create a string list and add strings to it.
- Write the code to iterate through the string list to process string characters.
- Run the application.
To create a VCL Form with TButton and TListBox controls
- Choose File > New > Other > Delphi Projects or C++Builder Projects and double-click the VCL Forms Application icon. The VCL Forms Designer is displayed.
- From the Standard page of the Tool palette, place two TButtons and a TListBox component on the form.
- Select Button1 on the form.
- In the Object Inspector, enter Add for the Name and Caption properties.
- Select Button2 on the form.
- In the Object Inspector, enter ToUpper for the Name and Caption properties.
To create a string list and add strings to it
- Select the Add button on the form.
- 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.
- For Delphi, place the cursor before the begin reserved word; then press ENTER. This creates a new line above the code block.
- For Delphi, insert the cursor on the new line created, and type the following variable declaration:
var MyList: TStringList;
- 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
- Select the ToUpper button on the form.
- 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.
- For Delphi, place the cursor before the begin reserved word; then press return. This creates a new line above the code block.
- For Delphi, insert the cursor on the new line created and type the following variable declaration:
var Index: Integer;
- 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
- Save your project files; then choose Run > Run to build and run the application. The form displays with the controls.
- Click the Add button. The strings 'Mice', 'Goats', 'Elephants', and 'Birds' display in the order listed.
- Click the ToUpper button. The string characters display in uppercase.