2012-01-08 108 views
2

我發佈了一個問題修復了Delphi中的窗體中的方法聲明中的錯誤,但是在修復了編譯時彈出的另一個錯誤,並且它的項目project1.exe引發了異常類EStringListError有消息「列表索引越界(0)」。當我按下繼續其不工作,但是當我按下打破代碼neraz:=true; 其閃爍,這是我下面列表索引越界(0)

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; 
+2

你的錯誤是'高清'。 – 2012-01-08 15:52:10

+0

什麼是'HD'? – 2012-01-08 15:52:49

+4

你的代碼是否真的格式化了?如果是這樣,那麼你迫切需要學習如何正確地縮進你的代碼。尤其是因爲您正在混合單個語句和複合語句塊。這使得人生在最好的時代變得艱難,但是沒有縮進紀律,你的代碼是不可維護的。 – 2012-01-08 15:56:06

回答

5

代碼的異常類EStringListError引發錯誤嘗試訪問空的TStrings實例的成員時,列表索引超出邊界(0)。最可能的候選對象是列表項的SubItems屬性。

你似乎陷入了一個相當普遍的陷阱。雖然您已爲列表視圖創建了列,但您還需要填寫每個列表項的SubItems列表。一個簡單的解決方案是修改HD這樣的:

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

儘管它實際上可能是更好地爲您創建的列表項,同時增加了分項目。但是我沒有顯示代碼,因爲你沒有包含填充列表的程序部分。

+0

你是這個人感謝它的工作 – 2012-01-08 16:48:03