-4

I want to make a loop so selected items in my listview delete from the directory.

procedure TFContact.BtnDeleteClick(Sender: TObject);
var
SecItem, BUFPath : string;
i : Integer;
begin
   if ListViewTab5.Selected <> nil then

    for i := 0 to ListViewTab5.Items.Count - 1 do
    begin
    SecItem:= TListViewItem(ListViewTab5.Selected).Text;
    BUFPath:= '/storage/emulated/0/Backup/'+SecItem;
    TFile.Delete(BUFPath);
    end
    else
    ShowMessage('File Deleted Succesfully');
end;
Ken White
  • 123,280
  • 14
  • 225
  • 444
Munir
  • 15
  • 3
  • What is your question? – David Heffernan Sep 14 '17 at 11:55
  • You mean checked, not selected, don't you? Btw. loop cannot have `else` branch. – Victoria Sep 14 '17 at 11:59
  • My loop is not working its only delete 1 item from ('/storage/emulated/0/Backup/') the selected items in the listview. – Munir Sep 14 '17 at 12:00
  • Hardly surprising. Your loop body makes no reference to the loop variables. It therefore deletes the same file over and again. It seems to me that today's novice programmers have, by and large, not learnt good debugging skills. Had you debugged this you would know the problem. Your real task here is to learn how to debug. Make that your top goal. – David Heffernan Sep 14 '17 at 12:03
  • yes i checked actually (i thought checked and selected are same) – Munir Sep 14 '17 at 12:04
  • @KenWhite Code compiles fine. What compiler error do you expect to see? – David Heffernan Sep 14 '17 at 13:02

1 Answers1

1

Try this:

procedure TFContact.BtnDeleteClick(Sender: TObject);
const
  Path = '/storage/emulated/0/Backup/';
var
  Item: TListViewItem;
begin
  for Item in ListView.Items do
    if Item.Checked then
      TFile.Delete(TPath.Combine(Path, Item.Text));
end;
Victoria
  • 7,822
  • 2
  • 21
  • 44
  • 1
    I asked question wrong because I was looking at wrong place. you have excellent skills to understand and guess my question. I must need to write properly so other may not need to guess – Munir Sep 14 '17 at 12:44
  • 1
    What you need to do, above everything else, is make an effort to learn how to debug. While you remain unable to debug, you will make little progress. – David Heffernan Sep 14 '17 at 12:59