Recent

Author Topic: Listview Delete Item Exception Error  (Read 395 times)

B1tbull

  • Newbie
  • Posts: 2
Listview Delete Item Exception Error
« on: December 14, 2019, 03:54:20 pm »
Greetings,

Environment is Windows 10 1903 , Lazarus 2.0.6.
Stepping up the learning curve for windows forms with a very basic listview example.
See attached project file. On Execution every button works, except delete selected which throws an exception in listitem.inc , function TListItem.GetChecked: Boolean; , line
LV := FOwner.FOwner; ( where LV turns out 0 ).
I did read a previous post on this and both ListView1.Items.Delete( i )
and ListView1.Items[ i ].Delete causes the exception.
Try the following : add 10 items, check 'Checkbox List View', check the first item in the list and click Del Selected -----> Exception Crash.

What am I missing ?

Thanks in advance
« Last Edit: December 14, 2019, 04:02:46 pm by B1tbull »

winni

  • Hero Member
  • *****
  • Posts: 723
Re: Listview Delete Item Exception Error
« Reply #1 on: December 14, 2019, 04:10:12 pm »
Hi!

Do not cut the branch you are sitting on!

You have to do it from the top to the bottom because otherwise you try to delete an item that does not exist anymore - because you have just deleted it.

 
Code: Pascal  [Select]
  1. for i := ListView1.Items.Count - 1 downto 0 do
  2.   begin
  3.    ......
  4.   end;

Winni

B1tbull

  • Newbie
  • Posts: 2
Re: Listview Delete Item Exception Error
« Reply #2 on: December 14, 2019, 04:30:56 pm »
Hi!

Do not cut the branch you are sitting on!

You have to do it from the top to the bottom because otherwise you try to delete an item that does not exist anymore - because you have just deleted it.

 
Code: Pascal  [Select]
  1. for i := ListView1.Items.Count - 1 downto 0 do
  2.   begin
  3.    ......
  4.   end;

Winni

 :o OMG , I feel so embarrassed.
Thanks a lot , you are truly a Hero

Francis

Birger52

  • Full Member
  • ***
  • Posts: 135
Re: Listview Delete Item Exception Error
« Reply #3 on: December 16, 2019, 12:13:43 am »
Code: Pascal  [Select]
  1. while ListView1.Items.Count > 0 do ...

winni

  • Hero Member
  • *****
  • Posts: 723
Re: Listview Delete Item Exception Error
« Reply #4 on: December 16, 2019, 12:55:32 am »
Code: Pascal  [Select]
  1. while ListView1.Items.Count > 0 do ...

Look into the code: your building an infinite loop!

Winni