Recent

Author Topic: List index (-1) out of bounds in ListBox  (Read 5596 times)

Przemyslav

  • New Member
  • *
  • Posts: 44
List index (-1) out of bounds in ListBox
« on: January 08, 2019, 09:51:03 pm »
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][+][-]
  1. unit Unit1;
  2. var
  3.   Form1: TForm1;
  4.   ulicekmp,ulicekmprecord : TStringList;

Code: Pascal  [Select][+][-]
  1. procedure TForm1.RadioGroup1Click(Sender: TObject);
  2. var
  3.    i : integer;
  4. begin
  5.      if (RadioGroup1.ItemIndex = 0) then
  6.      begin
  7.         if FileExists('dat\ulice0-9.dat') then
  8.         begin
  9.            ulicekmprecord:=TStringList.Create;
  10.            ulicekmprecord.Delimiter:=';';
  11.            ulicekmprecord.StrictDelimiter:=true;
  12.            ulicekmp:=TStringList.Create;
  13.            ulicekmp.StrictDelimiter:=true;ulicekmp.LoadFromFile('dat\ulice0-9.dat');
  14.            ListBox1.Enabled := true;
  15.            ListBox1.Clear;
  16.            Memo1.Clear;
  17.  
  18.            for i:=0 to ulicekmp.Count-1 do
  19.            begin
  20.               ulicekmprecord.DelimitedText:=ulicekmp.Strings[i];
  21.               ListBox1.Items.Add(ulicekmprecord.Strings[0]);
  22.            end;
  23.         end
  24.         else
  25.         MessageDlg('Nie odnaleziono pliku: ' + ExtractFilePath(Application.ExeName) + 'dat\ulice0-9.dat' + '.' , mtError, [mbOk], 0 );
  26.      end;
  27. (...)

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ListBox1Click(Sender: TObject);
  2. begin
  3.       ulicekmprecord.DelimitedText:=ulicekmp.Strings[ListBox1.ItemIndex];
  4.       Memo1.Enabled := true;
  5.       Memo1.Clear;
  6.       Memo1.Append(ulicekmprecord.Strings[1]);
  7. end;

So I have file for example "ulice0-9.dat" where I have somethong like this:

Quote
abcdefg;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

  • Hero Member
  • *****
  • Posts: 4219
Re: List index (-1) out of bounds in ListBox
« Reply #1 on: January 08, 2019, 10:02:03 pm »
Try this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ListBox1Click(Sender: TObject);
  2. begin
  3.   {Make sure there's an item selected before trying to use it}
  4.   if ListBox1.ItemIndex >= 0 then begin
  5.     ulicekmprecord.DelimitedText:=ulicekmp.Strings[ListBox1.ItemIndex];
  6.     Memo1.Enabled := true;
  7.     Memo1.Clear;
  8.     Memo1.Append(ulicekmprecord.Strings[1]);
  9.   end;
  10. end;

Nota that this is just a minimal check! You should also check that ItemIndex is a valid index for ulicekmp.Strings, etc.
« Last Edit: January 08, 2019, 10:03:59 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Przemyslav

  • New Member
  • *
  • Posts: 44
Re: List index (-1) out of bounds in ListBox
« Reply #2 on: January 08, 2019, 10:06:21 pm »
Thank you very much :) It's working in 2 cases - on item selected before and non selected. Thank you :)

 

TinyPortal © 2005-2018