Link to home
Start Free TrialLog in
Avatar of shekou
shekou

asked on

How to delete multi recorders in Listview?

I want to delete multi recorders in listview at same time, following is
my code:

   with mylistview1 do
   begin
     if items.Count=0 then exit;
     for i:= 0 to Items.Count-1 do
      if items[i].Selected then Items[i].Delete;
   end;

but get error message 'access violation
at address 0047A898', how to solve it?

I am using Delphi 5 and Win98.

Thanks
Shekou

Avatar of aubs
aubs

try doing it backwards
 with mylistview1 do
                         begin
                           if items.Count=0 then exit;
                           for i:= Items.Count-1 downto 0 do
                            if items[i].Selected then Items[i].Delete;
                         end;
Incidently you dont need this line:  if items.Count=0 then exit;

with mylistview1 do
begin
  for i:= Items.Count-1 downto 0 do
    if items[i].Selected then Items[i].Delete;
end;


Sorry an explanation:

suppose you have 5 items in the list that are all selected.
If you delete item[0] there are now only 4 items in the list.
If you delete item[1] there are now only 3 items in the list.
If you delete item[2] there are now only 2 items in the list.
So when you try to delete item[3], you will get an error because item[3] does not exist.

You need to start with the last item in the list, to stop this.

Regards Aubs
ASKER CERTIFIED SOLUTION
Avatar of jeurk
jeurk

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
don't look at the comments ;)
regards...
Avatar of shekou

ASKER

Thank you two, both way works.

Thanks

shekou
Hello,
just curious...
why only a B ?, the answer was with
source code and just a cut and paste
to make your problem work.
let me know please so I can enhance
my next answers...
Thanks.