Deleting Strings

From RAD Studio
Jump to: navigation, search

Go Up to How To Build Windows VCL Applications


Creating this VCL application consists of the following steps:

  1. Create a VCL Form with Buttons and ListBox controls.
  2. Write the code to add strings to a list.
  3. Write the code to delete a string from the list.
  4. Run the application.

To create a VCL Form with TButton and ListBox controls

  1. Choose File > New > Windows VCL Application - Delphi or Windows VCL Application - C++ Builder.
  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 Delete for the Name and Caption properties.

To add strings to a list

  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 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
  MyList: TStringList; 

For C++, enter the following variable declaration:

TStringList *MyList;
  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;
MyList = new TStringList();
try {
  MyList->Add( "Mice" );
  MyList->Add( "Goats" );
  MyList->Add( "Elephants" );
  MyList->Add( "Birds" );
  ListBox1->Items->AddStrings( MyList );
} __finally {
  MyList->Free();
}

To delete a string from the list

  1. Select the Delete 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.DeleteClick (Delphi) or TForm1::DeleteClick (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
  BIndex: Integer;

For C++, enter the following variable declaration:

int BIndex;
  1. For Delphi, insert the cursor within the code block and type the following code:
with ListBox1.Items do
    begin
      BIndex := IndexOf('Elephants');
      Delete (BIndex);
    end;

For C++

BIndex = ListBox1->Items->IndexOf( "Elephants" );
ListBox1->Items->Delete( BIndex );

To run the application

  1. Save your project files; then choose Run > Run 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 Delete button. The string 'Elephants' is deleted from the list.

See Also