Solved

How to delete multi recorders in Listview?

Posted on 2000-04-07
6
223 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month11 days, 15 hours left to enroll

623 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