Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SelectedIndexChanged事件在单击按钮时发生_C#_Winforms_Selectedindexchanged - Fatal编程技术网

C# SelectedIndexChanged事件在单击按钮时发生

C# SelectedIndexChanged事件在单击按钮时发生,c#,winforms,selectedindexchanged,C#,Winforms,Selectedindexchanged,我有一个删除列表框中所选项目的命令。执行删除命令后,我将如何更改对listbox另一项的选择?在button命令下写入此命令,它将删除listview中的选定项 listBox1.Items.Remove(listBox1.SelectedItem); 实际上,您想说的并不完整,但我得到的是,您希望删除列表框中当前选定的项;你可以这样做 int index=0; //Set its value corresponding to item you want to delete. listBox1

我有一个删除列表框中所选项目的命令。执行删除命令后,我将如何更改对listbox另一项的选择?

在button命令下写入此命令,它将删除listview中的选定项

listBox1.Items.Remove(listBox1.SelectedItem);

实际上,您想说的并不完整,但我得到的是,您希望删除列表框中当前选定的项;你可以这样做

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.
}
private void按钮1\u单击(对象发送者,事件参数e){
int i=列表框1.SelectedIndex;
如果(i>-1){
列表框1.Items.RemoveAt(i);
如果(listBox1.Items.Count>0)
listBox1.SelectedIndex=i
什么?问题不清楚。请将
SelectedIndex
设置为所需的任何索引,或将
SelectedItem
SelectedValue
设置为所需的索引。。
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;
  }
}