2 回答

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊
if (listBox1.Items[i].selected)
this.listBox1.Items.RemoveAt(i);
} 這樣明顯有問(wèn)題 你item里面有10個(gè)元素 你刪了3個(gè) 還有幾個(gè)? remove 1之后 原來(lái)的2就變成了1 原來(lái)的1被移除了 你在移除2 就是移除的是3
ListBox a1 = new ListBox();
object[] selected_objs = new object[a1.SelectedItems.Count];
a1.SelectedItems.CopyTo(selected_objs, 0);
foreach (object oval in selected_objs)
{
a1.Items.Remove(oval);
}
按我寫(xiě)的這樣弄吧

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超3個(gè)贊
方法1
void Btn_DeleteClick(object sender, System.EventArgs e)
{
ListBox.SelectedIndexCollection indices =this.listBox1.SelectedIndices;
int selected=indices.Count;
if(indices.Count>0)
{
for(int n=selected -1;n>=0;n--)
{
int index =indices[n];
listBox1.Items.RemoveAt(index);
}
}
}
方法2
void Btn_DeleteClick(object sender, System.EventArgs e)
{
for(int i=this.listBox1.Items.Count-1;i>=0;i--)
{
this.listBox1.Items.Remove(this.listBox1.SelectedItem);
}
}
- 2 回答
- 0 關(guān)注
- 1019 瀏覽
添加回答
舉報(bào)