Recent

Author Topic: TListView Problems with delete(Solved)  (Read 7313 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1293
TListView Problems with delete(Solved)
« on: April 05, 2019, 09:06:28 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.btnDeleteClick(Sender: TObject);
  2.  Var i : Integer;
  3. begin
  4.  i := Listview1.Items.Count;          //   i = 11 under the debugger
  5. // ListView1.Items.Delete(3);        //   out of bounds error
  6.  ListView1.ItemFocused.Free;         //  give a Sigsegv error
  7. end;  

Trying to learn how TListViews work. I have modified a demo which @wp wrote I think and trying to modify, add and delete rows without success.

The delete example comes from CodeCall.

Right now I do not understand why the above delete statements generate the errors.

Attached  a zip of the program with a small file.

I'm missing something but I have no idea what.

Need Help.
« Last Edit: April 07, 2019, 06:04:07 am by JLWest »
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

lucamar

  • Hero Member
  • *****
  • Posts: 4217
Re: TListView Problems with delete
« Reply #1 on: April 05, 2019, 09:34:36 pm »
Try with this:
Code: [Select]
  ListView1.Items[3].Delete;
But yes, your way should work too. I'll make a few tests and see what's going on ...
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: TListView Problems with delete
« Reply #2 on: April 05, 2019, 09:50:20 pm »
 ListView1.Items[3].Delete;

Compiles, however under the debugger it raises the  Sigsegv error and outside the debugger it crashes to the desktop without giving an error.
 
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

lucamar

  • Hero Member
  • *****
  • Posts: 4217
Re: TListView Problems with delete
« Reply #3 on: April 05, 2019, 10:14:56 pm »
Strange. Something else in your program is failing: I've just tested both ways and they both work as they should.

My test is simple: First populate a ListView with (random) 10..100  items; then select one and press Delete, rinse and repeat. Both ways to delete are used alternatively.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: TListView Problems with delete
« Reply #4 on: April 05, 2019, 10:38:00 pm »
My test is simple: First populate a ListView with (random) 10..100  items; then select one and press Delete, rinse and repeat. Both ways to delete are used alternatively.

What is "rinse"? 
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

lucamar

  • Hero Member
  • *****
  • Posts: 4217
Re: TListView Problems with delete
« Reply #5 on: April 05, 2019, 10:49:24 pm »
What is "rinse"?

Ah... sorry, it's a joke. "Rinse" means ducking in water something you're cleaning to clear out the soap: plates, clothes, whatever. Google "rinse and repeat" to see where that comes from. :-[

Just read this: Wikipedia - Lather, rinse and repeat
« Last Edit: April 05, 2019, 10:51:13 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

jamie

  • Hero Member
  • *****
  • Posts: 7707
Re: TListView Problems with delete
« Reply #6 on: April 05, 2019, 11:13:39 pm »
You deleted the entry so trying the Free it afterwards will result  in a bad call..

You need to test the results of ItemFocused, it maybe NIL....

so instead of deleting it first try this..

 if Boolean(ItemFocused) Then ItemFocused.Free;

 or
 If ItemFocused <> Nil Then ItemFocused.Free;

See what happens there.
 
The only true wisdom is knowing you know nothing

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: TListView Problems with delete
« Reply #7 on: April 05, 2019, 11:28:20 pm »
Ok I'll try it.

FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: TListView Problems with delete
« Reply #8 on: April 05, 2019, 11:40:02 pm »
 Had to change it to this to compile.
If Listview1.ItemFocused <> Nil Then Listview1.ItemFocused.Free;   


However it crashes to the desktop.

FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

lucamar

  • Hero Member
  • *****
  • Posts: 4217
Re: TListView Problems with delete
« Reply #9 on: April 05, 2019, 11:43:34 pm »
You deleted the entry so trying the Free it afterwards will result  in a bad call..
You need to test the results of ItemFocused, it maybe NIL....
so instead of deleting it first try this..
 if Boolean(ItemFocused) Then ItemFocused.Free;
 or
 If ItemFocused <> Nil Then ItemFocused.Free;
See what happens there.

What a way of complicating things. Much easier (and portable) to do:
Code: Pascal  [Select][+][-]
  1. if Assigned(ItemFocused) then ...

Even so, while it's true that after deleting an item ItemFocused may well be lost, that doesn't explain why Items.Delete(3) gives an Out of Bounds error when there are eleven items in the list.

ETA
Didn't see this, sorry!
ListView1.ItemFocused.Free; //  give a Sigsegv erro

IIRC, the items are owned by the list. Instead of freeing it just do:
Code: Pascal  [Select][+][-]
  1. ListView1.ItemFocused.Delete;
That should work right.
« Last Edit: April 05, 2019, 11:48:55 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

jamie

  • Hero Member
  • *****
  • Posts: 7707
Re: TListView Problems with delete
« Reply #10 on: April 05, 2019, 11:52:18 pm »
I just ran a test here..

 You do need to test for a NIL to start with because it there is not item select the itemFocus will be NIL..

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   if ListView1.ItemFocused <> Nil Then ListView1.ItemFocused.Free;
  4. end;                                                                  
  5.  

That works flawlessly here.

Laz 2.0.0
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 7707
Re: TListView Problems with delete
« Reply #11 on: April 06, 2019, 12:05:18 am »
Well, although I don't get any errors I did find a problem...

If you Select an item then the ItemFocus becomes that object..

If you unselect the selection to nothing, the ItemFocus still has the last item there and
if you delete it , it just moves to the next one..

 in other words, having nothing selected after the fact does not NIL the itemFocus.
that is a bug..
The only true wisdom is knowing you know nothing

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: TListView Problems with delete
« Reply #12 on: April 06, 2019, 12:07:08 am »
No I retested and I still get the Sigsegv error.

I have been working on just the delete for two days. I'm about ready to give up TListView.

The following is part of a toutorial on http://forum.codecall.net/topic/74040-basic-working-with-tlistview/ when he says "ListView1.ItemFocused.Free;" The code is safe, even when there is no focused item. Because calling Free on nil will not generate error.  If you want all currently selected rows to be deleted, just call the TListView's DeleteSelected method.

I haven't found  "TListView's DeleteSelected method"
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: TListView Problems with delete
« Reply #13 on: April 06, 2019, 12:11:31 am »
SearchResult :=  ListView1.items.indexof('SearchString');

I can;t get the indexof to work either.

It ws in the same tutorial.

Maybe TListViews are just to buggy to use.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

jamie

  • Hero Member
  • *****
  • Posts: 7707
Re: TListView Problems with delete
« Reply #14 on: April 06, 2019, 12:17:04 am »
start a blank project and drop a  TListView on it,..

Load it with a couple of items and then experiment with the code we have shown you..

its possible it may have something to do with how you are loading it.

use the object inspector to define a few entries...
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018