RAD Studio |  |

Creating Strings
See Also
Collapse All

Creating this VCL application consists of the following steps:

1. Create a VCL Form with TButton and TComboBox controls.
2. Write the code to create strings to the TButton OnClick handler.
3. Run the application.

To create a VCL Form with TButton and TComboBox 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 a TButton, a TLabel, and a TComboBox component on the form.

To write the create string procedure

1. Select Button1 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.Button1Click (Delphi) or TForm1::Button1Click (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 declarations:

Copy Code

    var
      StringList: TStrings;

For C++, enter the following variable declarations:

Copy Code

    TStrings *StringList;

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

Copy Code

    StringList := TStringList.Create;
      try
        with StringList do begin
          Add('Animals');
          Add('Cats');
          Add('Flowers');
        end;

        with ComboBox1 do begin
                            Width := 210;
          Items.Assign(StringList);
          ItemIndex := 0;
        end;

        Label1.Caption := 'Flowers has an index of ' +
          IntToStr( StringList->IndexOf( 'Flowers' ) );
      finally
        StringList.free;
      end;

Copy Code

    StringList = new TStringList();
    try {
     StringList->Add( “Animals” );
     StringList->Add( “Cats” );
     StringList->Add( “Flowers” );
     ComboBox1–>Width = 210;
     ComboBox1–>Items->Assign( StringList );
     ComboBox1–>ItemIndex = 0;
     Label1–>Caption = “Flowers has an index of “ +
       IntToStr( StringList->IndexOf( “Flowers” ) );
    } __finally {
         StringList->Free();
    }

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 Button. The strings 'Animals', 'Cars', and 'Flowers' display alphabetically in a list in the ListBox. The Label caption displays the message string: 'Flowers has an index value of 2.'
3. In the ComboBox, click the arrow to expand the drop-down list. The strings added in the TButton event handler appear.

See Also

VCL Overview

Building a VCL Forms Application

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