列表索引超出范围(0)

时间:2012-01-08 15:45:39

标签: delphi

我发布了一个关于修复Delphi中某个表单中的方法声明中的错误的问题,但在修复之后,编译时弹出了另一个错误,它说项目project1.exe引发异常类EStringListError,消息'list index out of bounds (0)'。当我按下继续它不工作但是当我按下它在代码neraz:=true;上闪烁时 这是我的代码

Procedure Reload;
var
    i:integer;
begin
form1.ListBox1.Clear;
form1.ListBox2.Clear;
if neraz then
HD;
neraz:=true;//..................here
form1.Label3.Caption:='free: '+inttostr(vs*32)+' byte'+#10#13+'cluster size = 32 bytes';
  i:=TABLE[nk1].nach;
   KolP1:=0; KolP2:=0;
   while (FAT[i]<>1024)  do begin
      if TABLE[fat[i]].tip then begin
          form1.ListBox1.Items.Add('dir>'+TABLE[fat[i]].name);
          inc(kolP1);
      end
      else
          if TABLE[fat[i]].format='txt' then
                form1.ListBox1.Items.Add('f_text> '+TABLE[fat[i]].name+'.'+TABLE[fat[i]].format)
          else
                form1.ListBox1.Items.Add('f_bin> '+TABLE[fat[i]].name+'.'+TABLE[fat[i]].format);
      if (fat[i]<>0) then
      i:=fat[i];
   end;
   i:=TABLE[nk2].nach;
   while (FAT[i]<>1024)  do begin
      if TABLE[FAT[i]].tip then begin
          form1.ListBox2.Items.Add('dir>'+TABLE[fat[i]].name);
          inc(kolP2)
      end
      else
          if TABLE[fat[i]].format='txt' then
                form1.ListBox2.Items.Add('f_text> '+TABLE[fat[i]].name+'.'+TABLE[fat[i]].format)
          else
                form1.ListBox2.Items.Add('f_bin> '+TABLE[fat[i]].name+'.'+TABLE[fat[i]].format);
      if (fat[i]<>0) then
      i:=fat[i];
   end;
   vfail;
end;


procedure HD;
var
  i: integer;
begin
  for i := 0 to 49 do begin
    with form2.ListView1.Items[i] do begin
      SubItems[0] := TABLE[i].name;
      SubItems[1] := TABLE[i].format;
      if TABLE[i].tip then
        SubItems[2] := 'folder'
      else
        SubItems[2] := 'file';
      SubItems[3] := IntToStr(TABLE[i].nach);
      SubItems[4] := IntToStr(TABLE[i].razmer);
    end;
    form2.ListView2.Items[i].SubItems[0] := IntToStr(fat[i]);
  end;
end;

1 个答案:

答案 0 :(得分:5)

当您尝试访问空EStringListError实例的成员时,异常类TStrings会引发错误列表索引超出范围(0)。最有可能的候选者是列表项的SubItems属性。

你似乎陷入了一个非常普遍的陷阱。虽然您已为列表视图创建了列,但您还需要为每个列表项填写SubItems列表。一个简单的解决方案是像这样修改HD

with form2.ListView1.Items[i] do begin
  while SubItems.Count<5 do
    SubItems.Add('');
  SubItems[0] := ...

尽管在创建列表项的同时添加子项实际上可能更好。但是我没有显示代码,因为你没有包含填充列表的程序部分。

相关问题