Lazarus

Programming => General => Topic started by: Wilko500 on March 31, 2021, 12:40:56 am

Title: [Solved] Access Violation When Clicking In TListView
Post by: Wilko500 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
Title: Re: Access Violation When Clicking In TListView
Post by: trev 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;
Title: Re: Access Violation When Clicking In TListView
Post by: Sieben 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;
Title: Re: Access Violation When Clicking In TListView
Post by: Wilko500 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.
Title: Re: Access Violation When Clicking In TListView
Post by: Gustavo 'Gus' Carreno 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
Title: Re: [Solved] Access Violation When Clicking In TListView
Post by: Wilko500 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!!!   
Title: Re: [Solved] Access Violation When Clicking In TListView
Post by: Gustavo 'Gus' Carreno 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
TinyPortal © 2005-2018