Forum > LCL

List index (-1) out of bounds in ListBox

(1/1)

Przemyslav:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1;var  Form1: TForm1;   ulicekmp,ulicekmprecord : TStringList;

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:=true;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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:


--- Quote ---abcdefg;12345
hjhgjgfjf;767666
rrturturtitr;7767676
(...)
--- End quote ---

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:
Try this:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:
Thank you very much :) It's working in 2 cases - on item selected before and non selected. Thank you :)

Navigation

[0] Message Index

Go to full version