[Delphi] ListView with Checkboxes!

Started by
2 comments, last by UltraMAX2K5 19 years, 3 months ago
Hi, can someone help me please, i've a problem with my ListView with checkboxes, i want to delete items that are checked only, how can i do that?! Forgive me my incorrect English. Thanks UltraMAX2K5
Advertisement
Hi, UltraMAX2K5

I think the only why of doing that is to loop through all your items and check whether or not the item is checked.

If it's checked then delete the current index of the items you are looping through and that should be it.

#edit: follow Forfaox code instead that of mine, did't thought about the renumbering.

var Loop: Integer;

for Loop := 0 to ListView1.Items.Count - 1 do begin
if ListView1.Items[Loop].Checked then
ListView1.Items.Delete(Loop);
end;

Hope it helps,

Starik

[Edited by - Starik1974 on January 5, 2005 7:51:25 AM]
I agree, although Starik's code won't work: When you delete an item, all items following the one deleted will be renumbered, so the posted code may skip deleting some items.

Use either:

for i:= ListView1.Items.Count-1 downto 0 do  if ListView1.Items.Checked then    ListView1.Items.Delete(i);


or:

i:= 0;while i<ListView1.Items.Count do  if ListView1.Items.Checked then    ListView1.Items.Delete(i)  else    Inc(i);
Thank you very much for both of you guys, it works very well and it was so simple to resolve that, but i'm not a expert in delphi but a amateur yet, thank you for your attention and precious help!

UltraMAX

This topic is closed to new replies.

Advertisement