有两个列表框,相互比较各自的条目,若listbox2中含有listbox1中的条目,则把listbox2列表框中该条目清除。我写了代码,可总报错,为什么?这是我所写的代码:   for i:=0 to ListBox2.Items.Count-1  do
   begin
      for j:=0 to listbox1.Items.Count-1 do
      begin
      if listbox2.Items.Strings[i]=listbox1.Items.Strings[j]
      then
      listbox2.Items.Delete(i);
     end;
   end;

解决方案 »

  1.   

    for j:=listbox1.Items.Count-1 downto 0 do
      

  2.   

    ListBox2.Items.Count-1  do
       begin
          for j:=listbox1.Items.Count-1 dowwnto  do
          begin
          if listbox2.Items.Strings[i]=listbox1.Items.Strings[j]
          then
          listbox2.Items.Delete(i);
         end;
       end;
      

  3.   

    这是一个边界问题啊,因为你删除了List中的一个item后,list.count减少了啊,所以最后肯定会告诉你出界啊,可以将循环改为从大到小
      

  4.   

    我现在改成这样了,还报错???
       for i:=listbox2.Items.Count-1 downto 0 do
       begin
          for j:=listbox1.Items.Count-1 downto 0 do
          begin
          if listbox2.Items.Strings[i]=listbox1.Items.Strings[j]
          then
          listbox2.Items.Delete(i);
          end;
       end;
      

  5.   

    噢,你是两个比较?这样估计不行,因为其中一个减少,另外一个不变.建议先记录下相同的项的位置,最后统一删除. ************************************************
       对不起,是我的网络慢,不是我诚心想copy楼上的  
     ************************************************