0

我有一个删除列表框中选定项目的命令。执行删除命令后如何更改列表框其他项目的选择?

4

3 回答 3

0

实际上你想说的并不完整,但我得到的是你想删除 ListBox 中当前选中的项目;你可以这样做;

int index=0; //Set its value corresponding to item you want to delete.
listBox1.Items.RemoveAt(index); //This will remove selected item.

现在,如果您希望在删除此项目后立即选择下一个项目,请添加此项目;

if(index!=-1)
{
   int nextindex=index+1; //Calculates the index of next item.
   listBox1.SelectedIndex=nextindex; //Selects next item.
}
于 2013-08-10T19:58:58.807 回答
0
private void button1_Click(object sender, EventArgs e) {
  int i = listBox1.SelectedIndex;
  if(i > -1) {
     listBox1.Items.RemoveAt(i);
     if(listBox1.Items.Count > 0)
       listBox1.SelectedIndex = i < listBox1.Items.Count ? i : listBox1.Items.Count - 1;
  }
}
于 2013-08-10T23:31:59.727 回答
0

在按钮命令下,写这个,它将删除列表视图中的选定项目。

listBox1.Items.Remove(listBox1.SelectedItem);
于 2013-08-10T19:45:39.510 回答