Copying a Complete String List (VCL)
Go Up to How To Build Windows VCL Applications
Copying a string list can have the effect of appending to or overwriting an existing string list. This VCL application appends to a string list. With a simple change, it can overwrite a string list. Creating this VCL application consists of the following steps:
- Create a VCL Form with TButtons, TComboBox, and TMemo controls.
- Write the code to create a string list to the Button1 OnClick handler.
- Write the code to copy the string list to the Button2 OnClick handler.
- Run the application.
To create a VCL Form with Button, ComboBox, and Memo controls
- Choose File > New > Windows VCL Application - Delphi or File > New > Windows VCL Application - C++Builder .
- From the Standard page of the Tool palette, place two TButtons, a TComboBox, and a TMemo component on the form.
To create the string list
- Select Button1 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.Button1Click (Delphi) or TForm1::Button1Click (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 declarations:
var StringList: TStrings;
For C++, enter the following variable declarations:
TStrings *StringList;
- Insert the cursor within the code block, and type the following code:
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 comboboxs list of strings.'); Add('Always remember: the TStrings.Create'); Add('method is abstract; use the'); Add('TStringList.Create method instead.'); end; with ComboBox1 do begin Width := 210; Items.Assign(StringList); ItemIndex := 0; end; finally StringList.free; end;
StringList = new TStringList(); try { 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( "Remember to call the TStringList constructor!" ); ComboBox1->Width = 210; ComboBox1->Items->Assign( StringList ); ComboBox1->ItemIndex = 0; } __finally { StringList->Free(); }
To copy the string list
- Select Button2 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.Button2Click (Delphi) or TForm1::Button2Click (C++) event handler block. At the cursor, enter the following code:
Memo1.Lines.AddStrings(ComboBox1.Items);
Memo1->Lines->AddStrings( ComboBox1->Items );
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 Button1.
- In ComboBox1, click the arrow to expand the drop-down list. The strings display in the TComboBox in the order listed in the event handler code for Button1.
- Click Button2. In the Memo1 window, the strings from ComboBox1 are appended to the 'Memo1' string.
Note: Try replacing the code in the Button2 event handler with the following code; then compile and run the application again.
Memo1.Lines.Assign(ComboBox1.Items);
Memo1->Lines->Assign( ComboBox1->Items );
The strings from ComboBox1 overwrite the 'Memo1' string.