Delphi,Lazarus - Listbox out of bound(0)TString

ThN*_*ThN -1 delphi listbox tstringlist lazarus indexoutofboundsexception

我似乎无法理解以下错误.

列表框超出范围(0)TString

我有一个带有列表框的表单或窗口,以下代码应该可以使用它.它假设从ini文件中获取字符串列表并设置为列表框.

IF selectedbox1count <> 0 then
BEGIN
   FOR i:=0 to selectedbox1count-1 do
      selectedbox.items[i] := AInifile.ReadString('DATAVIEW2', 'SHIFT1CHART'+(i+1), ' ');
END;
Run Code Online (Sandbox Code Playgroud)

但它总是在第一个实例到达该selectedbox.items[i]行时弹出一条错误消息.读取ini文件将返回"NEW CHART 2"字符串.我在这里错过了什么吗?

更新:selectedbox1count保存ini文件中的值...

Rem*_*eau 5

根据您的注释,您尝试将项目添加到ListBox,但该Items[]属性用于读取/修改现有项目.

你需要更像这样的东西:

SelectedBox.Items.BeginUpdate;
try
  SelectedBox.Items.Clear;
  SelectedBox1Count := AInifile.ReadInteger(...);
  for i := 0 to SelectedBox1Count-1 do
    SelectedBox.Items.Add(AInifile.ReadString('DATAVIEW2', 'SHIFT1CHART'+(i+1), ' '));
finally
  SelectedBox.Items.EndUpdate;
end;
Run Code Online (Sandbox Code Playgroud)