1. Home
  2. Computing & Technology
  3. Delphi Programming

Delete Multiple Selected Items in a TListBox Delphi Control

By Zarko Gajic, About.com

Delete Multiple Selected TListBox Items

Delete Multiple Selected TListBox Items

The TListBox Delphi control displays collection of strings in a scrollable list. By setting the MultiSelect property to true, the user can select more than one item at a time.

How to Remove Selected ListBox Items

When MultiSelect is true, the user can select multiple items in the control, and the SelCount property indicates the number of selected items.

To remove all the selected items from the list box you need to call the Delete method of the underlying TStrings object.

Since Delete changes the ordinal position of the remaining items in the list, when deleting items from the list using the for loop you need to start iterating from the end of he list. The Selected property tells you if an item at a particular index is selected.

Here's the code to delete multiple selected items in the list box:

//make sure ListBox1.MultiSelect = true
var
  ii : integer;
begin
  with ListBox1 do
  begin
    for ii := -1 + Items.Count downto 0 do
    if Selected[ii] then Items.Delete(ii) ;
  end;
end;
Delphi tips navigator:
» TObject(Sender) vs. (Sender as TObject) - Differences Between a Hard Cast and an AS Cast in Delphi
« Implement the On Item Checked Event for TListView
More Delphi Programming Quick Tips
Zarko Gajic
Guide since 1998

Zarko Gajic
Delphi Programming Guide

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Using VCL Components
  5. TListBox
  6. Delete Multiple Selected Items in a TListBox Delphi Control

©2009 About.com, a part of The New York Times Company.

All rights reserved.