Solved

How to delete multi recorders in Listview?

Posted on 2000-04-07
6
204 Views
Last Modified: 2010-04-04
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

0
Comment
Question by:shekou
  • 3
  • 2
6 Comments
 
LVL 2

Expert Comment

by:aubs
ID: 2693069
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;
0
 
LVL 2

Expert Comment

by:aubs
ID: 2693100
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
0
 
LVL 4

Accepted Solution

by:
jeurk earned 100 total points
ID: 2693112
hello you should it like that :

procedure TForm1.Button3Click(Sender: TObject);
var
  liSelected, liBuffer: TListItem; //the item actualy selected in the listview
begin
  liSelected := mylistview1.Selected;
  if liSelected = nil then
    exit; //shoul not be possible

  while mylistview1.SelCount <> 0 do
  begin
    //delete the selected Items because they no longer exists
    liBuffer := liSelected;
    //get the next selevted item
    liSelected := mylistview1.GetNextItem(liSelected, sdAll, [isSelected]);
    liBuffer.Delete;
  end;
end;
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 4

Expert Comment

by:jeurk
ID: 2693115
don't look at the comments ;)
regards...
0
 

Author Comment

by:shekou
ID: 2695841
Thank you two, both way works.

Thanks

shekou
0
 
LVL 4

Expert Comment

by:jeurk
ID: 2699524
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.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now