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.pngMy code:
unit Unit1;
var
Form1: TForm1;
ulicekmp,ulicekmprecord : TStringList;
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;
(...)
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:
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.pngHow to remove this error?
Sorry for my English, thank you for your help