1. Computing

Delete Multiple Selected Items in a TListBox Delphi Control

From , former About.com Guide

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

©2013 About.com. All rights reserved.