为什么SelectedIndices,只能取得多选项中的第一个index值?

fpstar 2008-07-23 06:15:44
我的源代码是这样,不知道为什么当i等于1时循环就终止了,说是超出范围。请大侠指点一下!

private void ImageFileClearButton_Click(object sender, System.EventArgs e)
{
int SelectedCount = this.SelectFileTextBox.SelectedIndices.Count;
for(int i = 0; i<SelectedCount; i++){
int index = this.SelectFileTextBox.SelectedIndices[i];
if (index >= 0)
{
this.SelectFileTextBox.Items.RemoveAt(index);
this.files.RemoveAt(index);
}
}



if(this.SelectFileTextBox.Items.Count ==0)
{
this.ImageFileClearButton.Enabled = false;
this.ImageOpenFileButton.Enabled = false;
}
}
...全文
145 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
江城老温 2008-07-23
  • 打赏
  • 举报
回复
for循环的时候总元素是不能减少的。否则会出错。这是很基本的逻辑问题。仔细检查就能看出来了。
fpstar 2008-07-23
  • 打赏
  • 举报
回复
倒过来删除就可以了
fpstar 2008-07-23
  • 打赏
  • 举报
回复
我明白了,只要倒着删除就可以了。因为每次删除Item之后,ListViewItems和SelectedItems都会发生变化。
int SelectedCount = this.SelectFileTextBox.SelectedIndices.Count;
for(int i = 0; i<SelectedCount; i++){
int j = SelectedCount - i - 1;
int index = this.SelectFileTextBox.SelectedIndices[j];
if (index >= 0)
{
this.SelectFileTextBox.Items.RemoveAt(index);
this.files.RemoveAt(index);
}
}

110,538

社区成员

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

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

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