日期:2014-05-17 浏览次数:20993 次
if (listBox1.SelectedItems.Count == 0) { MessageBox.Show("Please select the items you want to delete"); } else { listBox1.Items.Remove(listBox1.SelectedItems); }
if (listBox1.SelectedItems.Count == 0) { MessageBox.Show("Please select the items you want to delete"); } else { foreach (var item in listBox1.SelectedItems) { listBox1.Items.Remove(item); } }
------解决方案--------------------
删除指定索引
------解决方案--------------------
3楼正确
.SelectedItems 是集合
remove是单项,需要循环处理
------解决方案--------------------
if (listBox1.SelectedItems.Count == 0) { MessageBox.Show("Please select the items you want to delete"); } else { for(int i = this.listBox1.SelectedItems.Count-1; i>=0;i--) { this.listBox1.Items.Remove(this.listBox1.SelectedItems[i]); } }