List index (-1) out of bounds in ListBox lazarus / 31-LCL ---------------------------------------------------------------------- Przemyslav · 2019-01-08 20:51 Hello everybody. I have problem with my ListBox. I get error like this: "List index (-1) out of bounds", when I click on empty space in ListBox. I have form like this: https://i.postimg.cc/HW4H5HR3/Bez-tytu-u.png My code: 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 unit Unit1;var  Form1: TForm1;   ulicekmp,ulicekmprecord : TStringList; 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.RadioGroup1Click(Sender: TObject);var   i : integer;begin     if (RadioGroup1.ItemIndex = 0) then     begin        if FileExists('dat\ulice0-9.dat') then        begin           ulicekmprecord:=TStringList.Create;           ulicekmprecord.Delimiter:=';';           ulicekmprecord.StrictDelimiter:=true;           ulicekmp:=TStringList.Create;           ulicekmp.StrictDelimiter:=tru e;ulicekmp.LoadFromFile('dat\ulice0-9.dat');           ListBox1.Enabled := true;           ListBox1.Clear;           Memo1.Clear;            for i:=0 to ulicekmp.Count-1 do           begin              ulicekmprecord.DelimitedText:=ulicekmp.Strings[i];              ListBox1.Items.Add(ulicekmprecord.Strings[0]);           end;        end        else        MessageDlg('Nie odnaleziono pliku: ' + ExtractFilePath(Application.ExeName) + 'dat\ulice0-9.dat' + '.' , mtError, [mbOk], 0 );     end;(...) 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.ListBox1Click(Sender: TObject);begin      ulicekmprecord.DelimitedText:=ulicekmp.Strings[ListBox1.ItemIndex];    Memo1.Enabled := true;      Memo1.Clear;      Memo1.Append(ulicekmprecord.Strings[1]);end; So I have file for example "ulice0-9.dat" where I have somethong like this: Quoteabcdefg;12345 hjhgjgfjf;767666 rrturturtitr;7767676 (...) My file is opened in RadioGroup and imported to 2 stringlists ulicekmp,ulicekmprecord with delimited text (. One part is imported to items of ListBox. When I click on non empty item in ListBox, my program display second part on Memo. I have error "List index (-1) out of bounds" when I click on empty space in ListBox after imported values: https://i.postimg.cc/dDPTzFR5/Bez-tytu-u.png How to remove this error? Sorry for my English, thank you for your help ---------------------------------------------------------------------- lucamar · 2019-01-08 21:02 Try this: 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.ListBox1Click(Sender: TObject);begin  {Make sure there's an item selected before trying to use it}  if ListBox1.ItemIndex >= 0 then begin    ulicekmprecord.DelimitedText:=ulicekmp.Strings[ListBox1.ItemIndex];  Memo1.Enabled := true;    Memo1.Clear;    Memo1.Append(ulicekmprecord.Strings[1]);  end;end; Nota that this is just a minimal check! You should also check that ItemIndex is a valid index for ulicekmp.Strings, etc. ---------------------------------------------------------------------- Przemyslav · 2019-01-08 21:06 Thank you very much It's working in 2 cases - on item selected before and non selected. Thank you