Delphi Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreDelphi Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 27th, 2004, 01:08 AM
Biopsy Biopsy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 7 Biopsy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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!

Reply With Quote
  #2  
Old August 27th, 2004, 01:28 AM
rossM rossM is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: England
Posts: 952 rossM User rank is Corporal (100 - 500 Reputation Level)rossM User rank is Corporal (100 - 500 Reputation Level)rossM User rank is Corporal (100 - 500 Reputation Level)rossM User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 23 h 38 m 44 sec
Reputation Power: 13
Try using Listview1.DeleteSelected; instead.

Reply With Quote
  #3  
Old August 27th, 2004, 01:33 AM
Biopsy Biopsy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 7 Biopsy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
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.

Reply With Quote
  #4  
Old August 27th, 2004, 01:40 AM
rossM rossM is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: England
Posts: 952 rossM User rank is Corporal (100 - 500 Reputation Level)rossM User rank is Corporal (100 - 500 Reputation Level)rossM User rank is Corporal (100 - 500 Reputation Level)rossM User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 23 h 38 m 44 sec
Reputation Power: 13
Does it have a Selected property?
Code:
Listview1.Selected.Delete;

Reply With Quote
  #5  
Old August 27th, 2004, 01:46 AM
Biopsy Biopsy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 7 Biopsy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
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!!

Reply With Quote
  #6  
Old August 27th, 2004, 02:08 AM
rossM rossM is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: England
Posts: 952 rossM User rank is Corporal (100 - 500 Reputation Level)rossM User rank is Corporal (100 - 500 Reputation Level)rossM User rank is Corporal (100 - 500 Reputation Level)rossM User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 23 h 38 m 44 sec
Reputation Power: 13
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;

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreDelphi Programming > Deleting selected item in TListView

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap