Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

delete items in listview

Status
Not open for further replies.

safra

Technical User
Jan 24, 2001
319
NL
I have two problems with deleting multiple (selected) items in a listview.

Code:
with ListView1 do
 for j := 0 to Items.Count - 1 do
  if Items[j].Selected then
   ListView1.Items[j].Delete;

The problems are caused by the fact that apparantly it is not possible to manipulate the j integer (decrease it by 1 after finding a selected record) and that apparantly Items.Count in the for statement is not updated after deleting an item which results in errors at the end of the list.

The last problema I solved with:

Code:
with ListView1 do
 for j := 0 to Items.Count - 1 do
  if j < Items.Count then
    if Items[j].Selected then
     lvHeaders.Items[j].Delete;

After deleting an item, all items below move up. How can I keep i on the same value after deleting a record to check the state of the next item that now has the index of the deleted item?
 
Try starting from the end of the items list:
Code:
with ListView1 do
 for j := Items.Count - 1 downto 0 do
  if Items[j].Selected then
   ListView1.Items[j].Delete;

That way you don't have to worry about what's been reordered because it's already been processed.

-D

 
I tried that using:

for j := Items.Count - 1 to 0 do

But it didn't work. Didn't know about 'downto',

Thanks!
Raoul
 
Hi,

I may be being thick, but can you not just use...

ListView1.DeleteSelected;


There are two ways to write error-free programs; only the third one works.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top