#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    7
    Rep Power
    0

    Deleting selected item in TListView


    For some reason, when i try to delete a selected item in the TListView, i get EAccessViolation Error. But the code still deletes the selected item. Here is what i have.

    Code:
    procedure TForm1.Button6Click(Sender: TObject);
    var
      i: integer;
    begin
    try
      with Listview1 do
        for i := 0 to Items.Count - 1 do
          if Items[i].Selected then
          Items.Delete(Items.IndexOf(FindCaption(0, Items[i].Caption, False, True, False)));
            except on EAccessViolation do
            // silent
            end;
            end;
    I had to catch the exception to make it stop showing that error message. But why is it causing it? The code seems to be fine and logical. Any help would be appreciated. Thank you!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    England
    Posts
    952
    Rep Power
    14
    Try using Listview1.DeleteSelected; instead.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    7
    Rep Power
    0
    Originally Posted by rossM
    Try using Listview1.DeleteSelected; instead.
    It doesn't have that property. Must be a new version of TListView. I'm using Delphi 5.
    All it has is the Delete(); procedure.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    England
    Posts
    952
    Rep Power
    14
    Does it have a Selected property?
    Code:
    Listview1.Selected.Delete;
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    7
    Rep Power
    0
    Originally Posted by rossM
    Does it have a Selected property?
    Code:
    Listview1.Selected.Delete;
    haha... jesus, i am an idiot. I don't know how i didn't see that. lol... I still wonder why my method didn't work though.
    but thanks a lot!!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    England
    Posts
    952
    Rep Power
    14
    It's because when you delete an item, Items.Count reduces by 1 but the number of iterations of the for loop does not decrease and you get the exception when trying to access an item past the end of the list. If you use a while loop like this then it works:
    Code:
    var
      i: Integer;
    begin
      with Listview1 do
      begin
        i := 0;
        while i < Items.Count do
          if Items[i].Selected then
            Items.Delete(Items.IndexOf(
              FindCaption(0, Items[i].Caption, False, True, False)))
          else
            Inc(i);
      end;

IMN logo majestic logo threadwatch logo seochat tools logo