//从ListBox中删除选中的选项
        private void btnDelete_Click(object sender, EventArgs e)
        {   
            
            if (this.listBox.SelectedItems.Count>0)
            {
                foreach (string item in this.listBox.SelectedItems                
                {
                    this.listBox.Items.Remove(item);
                }
            }
        }
运行时在in那里提示出错:此枚举数绑定到的列表已被修改,仅在列表没有更改时才能使用枚举数.
不知道怎么修改

解决方案 »

  1.   

    你找到这项后先记下index,然后在退出循环后再remove
      

  2.   

    不要用foreach,用for,而且按照索引由大到小的順序,就不會出錯了.foreach在循環體内部,不能修改集合的
      

  3.   

    // 从后往前删除
    private void button1_Click(object sender, System.EventArgs e)
    {

    for(int i = this.listBox1.Items.Count - 1 ; i >= 0 ; i --)
    {
    if(this.listBox1.SelectedIndices.Contains(i))
    {
    this.listBox1.Items.RemoveAt(i);
    }
    }
    }
      

  4.   

    for( int i = listBox.SelectedItems.Count; i > 0; i --)
    {
     this.listBox.Items.RemoveAt(i);
    }
      

  5.   

    private void btnDelete_Click(object sender, EventArgs e)
            {   
                
                if (this.listBox.SelectedItems.Count>0)
                {
                    for(int i = this.listBox.Items.Count -1;i > 0;i--)
                    {
                        this.listBox.Items.Remove(listBox.SelectedItem);
                    }
                }
            }
    listBox的Remove方法要的参数是一个Object类型的,所以要把listBox的Item传进去。