procedure TmainFrm.Button1Click(Sender: TObject);
begin
      if listbox1.Items.Count>0 then
                ListBox1.Items.Delete(ListBox1.ItemIndex);
end;
后,出现list index out of bound(-1)的错误,怎么消除这个错误!先用选中一项再按钮就没事,谢谢!

解决方案 »

  1.   

    把条件
    if listbox1.Items.Count>0 then
    换成
    if (listbox1.Items.Count>0) and (listbox1.itemindex>=0) then
    试试看
      

  2.   

    刚才没有看清楚题目,这个ItemIndex表示的是你当前选择的列表项,如果没有选择,这个数值是-1。你调用delete方法删除一个不合法的列表项,当然会被提示list index out of bound(-1)
      

  3.   

    if listbox1.ItemIndex<>-1 then
                    ListBox1.Items.Delete(ListBox1.ItemIndex);
      

  4.   

    上面的也不行。就是当先没有选择listbox, 然后就直接对它操作就会这样。
    大家再帮我看看吧!
      

  5.   

    procedure TForm1.Button1Click(Sender: TObject);
    begin
      if listbox1.ItemIndex<-1 then
        ListBox1.Items.Delete(ListBox1.ItemIndex);
    end;如果还是有错就是其他问题了
      

  6.   

    procedure TForm1.Button1Click(Sender: TObject);
    begin
      if listbox1.ItemIndex>=0 then
        ListBox1.Items.Delete(ListBox1.ItemIndex);
    end;如果还是有错就是其他问题了