for i:=0 to  frmbook.ListBox1.Items.Count-1  do
  begin
    if frmbook.ListBox1.Items[i]=zh.Text then
    frmbook.ListBox1.Items.Delete(i) ;
  end;
出现以下异常:
class with message estringlisterror
'list index out of bounds(1)'
如何解决?

解决方案 »

  1.   

    这是因为你的i的值可能已经超出了listbox1.items.count的范围,你可以把程序改成这样:
    Var k:integer;
    k:=frmbook.ListBox1.Items.Count-1;
    for jsq:=0 to k do
    begin
    if frmbook.ListBox1.Items[i]:=zh.Text then
       begin
         frmbook.ListBox1.Items.Delete(i);
         K:=K-1;//将K的值减1,因为列表框的项目数已经少了一项了。
       end;
    end;
      

  2.   

    你可以倒着走 呵呵for i:=frmbook.ListBox1.Items.Count-1 downto 0 do
      begin
        if frmbook.ListBox1.Items[i]=zh.Text then
        frmbook.ListBox1.Items.Delete(i) ;
      end;
      

  3.   

    比如frmbook.ListBox1.Items.Count为3,则循环条件是 for i:=0 to 2
    当你删除了一行之后,frmbook.ListBox1.Items.Count[2]就越界了