listview的remove问题

swsw1 2003-08-22 11:24:54
for (int i=0;i<listView1.CheckedIndices.Count;i++)
{
listView1.Items.Remove(listView1.CheckedItems[i]);
}
为何每次执行时都有一条删除不了(选择三条只能删除两条)
...全文
82 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
rouser 2003-08-22
  • 打赏
  • 举报
回复
樓上的和樓上上的說的都對
:)

再給你一種麻煩的
ListViewItem[] items = new ListViewItem[this.listView1.CheckedItems.Count];
for(int i=0;i<this.listView1.CheckedItems.Count;i++)
items[i] = this.listView1.CheckedItems[i];
foreach(ListViewItem item in items)
this.listView1.Items.Remove(item);
tibet 2003-08-22
  • 打赏
  • 举报
回复
del时要从后向前删
janus1999 2003-08-22
  • 打赏
  • 举报
回复
while(listView1.CheckedItems.Count >0)
listView1.Items.Remove(listView1.CheckedItems[0]);
rouser 2003-08-22
  • 打赏
  • 举报
回复
ListView.ListViewItemCollection items = listView1.CheckedItems;
foreach(ListViewItem item in items)
{
listView1.Items.Remove(item);
}
jjcccc 2003-08-22
  • 打赏
  • 举报
回复
其实最好不用索引来连续删除集合项,因为在在删除的同时,集合会自动调整项的索引。
rouser 2003-08-22
  • 打赏
  • 举报
回复
所以CheckItems[i]就變成CheckItems[i++]了

greenhill 2003-08-22
  • 打赏
  • 举报
回复
for (int i=0;i<listView1.CheckedIndices.Count-1;i++)
{
listView1.Items.Remove(listView1.CheckedItems[i]);
}
rouser 2003-08-22
  • 打赏
  • 举报
回复
因為你沒刪除一條,CheckedIndices.Count就自動--了

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧