Recent

Author Topic: [Solved] Access Violation When Clicking In TListView  (Read 836 times)

Wilko500

  • New Member
  • *
  • Posts: 34
[Solved] Access Violation When Clicking In TListView
« on: March 31, 2021, 12:40:56 am »
I have a project with three ListView controls.  If there is clear space below the last item in the ListView and I click (accidentally) on that blank space 2 of the 3 give access violation error.

I have simulated this in the attached project. The error is caused by the line
Code: Pascal  [Select][+][-]
  1. s:=ListView1.Selected.Caption;
in the following
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ListView1Click(Sender: TObject);
  2. Var
  3.   s:    String;
  4. begin
  5.     s:=ListView1.Selected.Caption;
  6.     ShowMessage(s);
  7. end;

I would have expected that with the onclick event nothing would occur unless an item is clicked.  Evidently not the case. The solution I have come up with is
Code: Pascal  [Select][+][-]
  1.  
  2. procedure TForm1.ListView1Click(Sender: TObject);
  3. Var
  4.   s:    String;
  5.   i:    Integer;
  6. begin
  7.     i:=ListView1.ItemIndex;
  8.     if i >-1 then
  9.        Begin
  10.        s:=ListView1.Selected.Caption;
  11.        ShowMessage(s)
  12.        End;
  13. end;
  14.  
While this works it seems a bit clumsy.  Is there a better way of getting the caption for the selected item?
Thanks
« Last Edit: April 01, 2021, 11:38:57 pm by Wilko500 »

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1474
  • Former Delphi 1-7, 10.2 user
Re: Access Violation When Clicking In TListView
« Reply #1 on: March 31, 2021, 01:42:28 am »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ListView1Click(Sender: TObject);
  2. begin
  3.     if(ListView1.ItemIndex > -1) then
  4.        ShowMessage(ListView1.Selected.Caption)
  5. end;
Lazarus 2.1 r65061 FPC 3.3.1 r49223 macOS 10.14.6 Xcode 11.3.1
Lazarus 2.1 r65182 FPC 3.3.1 r49223 macOS 11.4 aarch64 Xcode 12.4
Lazarus 2.1 r61574 FPC 3.3.1 r42318 FreeBSD 12.1 amd64 VMware VM
Lazarus 2.1 r61574 FPC 3.0.4 Ubuntu 20.04 Parallels VM
Lazarus 2.0.10 FPC 3.2.0 Win10 Parallels VM

Sieben

  • Full Member
  • ***
  • Posts: 215
Re: Access Violation When Clicking In TListView
« Reply #2 on: March 31, 2021, 02:04:17 am »
Code: Pascal  [Select][+][-]
  1.     procedure TForm1.ListView1Click(Sender: TObject);
  2.     begin
  3.       if Assigned(ListView1.Selected) then
  4.         ShowMessage(ListView1.Selected.Caption)
  5.     end;
Lazarus 2.0.10, FPC 3.2.0, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

Wilko500

  • New Member
  • *
  • Posts: 34
Re: Access Violation When Clicking In TListView
« Reply #3 on: April 01, 2021, 11:35:11 pm »
I'm not sure I understand the reason for the error. I have another ListView that does not error when blank space is clicked but in that case I do not access a list view item.  Seems to me that the OnClick event is fired when the listView control is clicked whereas I would have expected that OnClick should fire when an item is clicked and be ignored when space is clicked.

Nevertheless both suggestions work fine thank you.

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 535
  • Professional amateur ;-P
Re: Access Violation When Clicking In TListView
« Reply #4 on: April 02, 2021, 12:38:07 am »
Hey Wilko,

I'm not sure I understand the reason for the error. I have another ListView that does not error when blank space is clicked but in that case I do not access a list view item.  Seems to me that the OnClick event is fired when the listView control is clicked whereas I would have expected that OnClick should fire when an item is clicked and be ignored when space is clicked.

When you make the TListview not have any selected item, in the case of clicking in a white space, it then assigns the value -1 to the TListview.ItemIndex and, most probably, will assign nil to TListview.Selected.

So if any event is triggered after the TListview as done that, you will always have an error when you access TListview.Selected, since it's now invalid.

Get it now?

Cheers,
Gus
Lazarus 2.1.0(trunk) FPC 3.3.1(trunk) Ubuntu 21.04 64b Dark Theme
Lazarus 2.0.12(stable) FPC 3.2.2(stable) Ubuntu 21.04 64b Dark Theme
http://github.com/gcarreno

Wilko500

  • New Member
  • *
  • Posts: 34
Re: [Solved] Access Violation When Clicking In TListView
« Reply #5 on: April 02, 2021, 12:47:23 am »
Thanks Gus, yes, you’ve supplied the missing link.   Actually, it might be that I’m the missing link!!!   

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 535
  • Professional amateur ;-P
Re: [Solved] Access Violation When Clicking In TListView
« Reply #6 on: April 02, 2021, 02:44:35 am »
Hey Wilko,

Thanks Gus, yes, you’ve supplied the missing link.   Actually, it might be that I’m the missing link!!!

Hey, hey, don't say that!! Don't say bad things about you like that!!

We all have to learn. I, also, had to learn until I got to the level I got.
And I got to this level by learning and being taught.
Learning by myself and being taught by other patient ones.

Sometimes on these forums, you get less patient people that won't have the patience to explain to the ones that are just initiating the language.
I still remember being in that stage and I always strive to ease that pain of having no clue what you're doing.

So I'm glad you learned something today, but please don't pull yourself down like that!!

Cheers,
Gus
Lazarus 2.1.0(trunk) FPC 3.3.1(trunk) Ubuntu 21.04 64b Dark Theme
Lazarus 2.0.12(stable) FPC 3.2.2(stable) Ubuntu 21.04 64b Dark Theme
http://github.com/gcarreno

 

TinyPortal © 2005-2018