[Solved] Access Violation When Clicking In TListView lazarus / 21-General ---------------------------------------------------------------------- Wilko500 · 2021-03-30 22:40 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i s:=ListView1.Selected.Caption; in the following Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.ListView1Click(Sender: TObject);Var  s:    String;begin    s:=ListView1.Selected.Caption;    ShowMessage(s);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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  procedure TForm1.ListView1Click(Sender: TObject);Var  s:    String;  i:    Integer;begin    i:=ListView1.ItemIndex;    if i >-1 then       Begin       s:=ListView1.Selected.Caption;       ShowMessage(s)       End;end;  While this works it seems a bit clumsy.  Is there a better way of getting the caption for the selected item? Thanks ---------------------------------------------------------------------- trev · 2021-03-30 23:42 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.ListView1Click(Sender: TObject);begin    if(ListView1.ItemIndex > -1) then       ShowMessage(ListView1.Selected.Caption)end; ---------------------------------------------------------------------- Sieben · 2021-03-31 00:04 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     procedure TForm1.ListView1Click(Sender: TObject);    begin      if Assigned(ListView1.Selected) then        ShowMessage(ListView1.Selected.Caption)    end; ---------------------------------------------------------------------- Wilko500 · 2021-04-01 21:35 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 · 2021-04-01 22:38 Hey Wilko, Quote from: Wilko500 on April 01, 2021, 11:35:11 pmI'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 ---------------------------------------------------------------------- Wilko500 · 2021-04-01 22:47 Thanks Gus, yes, you’ve supplied the missing link.   Actually, it might be that I’m the missing link!!! ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2021-04-02 00:44 Hey Wilko, Quote from: Wilko500 on April 02, 2021, 12:47:23 amThanks 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