ListBox设置为MultiExtended,界面中有一个按钮允许删除选择的Item,但我总是不能一次性删除所选的全部,我的实现代码如下:private void btnDel_Click(object sender, System.EventArgs e)
{
    for(int count = 0;count < this.lstSampleFields.SelectedItems.Count;count++)
    {
        this.lstSampleFields.Items.Remove(this.lstSampleFields.Items[count]);
    }
    this.lstSampleFields.Refresh();
}不知道哪里出了问题,请帮帮忙吧~~~谢谢!

解决方案 »

  1.   

    for (int i = 0; i < ListBox1.Items.Count - 1; i++)
            {
                ListBox1.Items.Clear();//.Remove(ListBox1.Items);
            }
      

  2.   

    for(int i=0;i<this.ListBox1.Items.Count;i++)
    {
    if(this.ListBox1.Items[i].Selected)
    this.ListBox1.Items.Remove(this.ListBox1.Items[i]);
    }
      

  3.   

    你出错在。count从0开始,但是listbox被选中的item的index不一定是和count一样的哦。
    呵呵。
      

  4.   

    To:elivehai
    呃对哦,索引不一样呃
    你代码里的this.ListBox1.Items[i].Selected,貌似么有这个属性哦?点不出来
    敲进去编译报错说Selected未定义按照这个解决思路分析,即使用IndexOf得到真实的index,如果用循环,删掉第一个后剩下的选项索引也会随之改变,还是不行。难道一定要提取选项的值来进行操作?
      

  5.   

    if (this.listBox1.SelectedItems != null)
                {
                    while (this.listBox1.SelectedItems.Count > 0)
                        this.listBox1.Items.Remove(this.listBox1.SelectedItems[0]);
                }
      

  6.   

    多谢 syfsz 兄,这样果然简单,我自己用笨方法也实现了。