c#从相应的列表框中删除项目

c#从相应的列表框中删除项目,c#,listbox,listboxitem,event-driven,C#,Listbox,Listboxitem,Event Driven,所以我有三个列表框,目标是一旦用户单击一个列表框中的一个项目并点击表单上的删除按钮,它就会删除该项目,然后在同一级别上删除其他两个列表框中的项目 因此,删除列表框1的第五个元素将删除列表框2和3的第五个元素 private void btnDelete_Click(object sender, EventArgs e) { lstBox1.Items.Remove(lstBox1.SelectedItem); lstBox2.Items.Remove(lstBox2.Select

所以我有三个列表框,目标是一旦用户单击一个列表框中的一个项目并点击表单上的删除按钮,它就会删除该项目,然后在同一级别上删除其他两个列表框中的项目

因此,删除列表框1的第五个元素将删除列表框2和3的第五个元素

private void btnDelete_Click(object sender, EventArgs e)
{
    lstBox1.Items.Remove(lstBox1.SelectedItem);
    lstBox2.Items.Remove(lstBox2.SelectedItem);
    lstBox3.Items.Remove(lstBox3.SelectedItem);
}
因此,根据我目前所掌握的,它将从一个列表框中删除一个项目,但显然没有什么可以处理从其他列表框中删除项目的问题


有什么想法吗

如果所说的
同一级别
,您指的是索引,那么可以很容易地这样做:

private void btnDelete_Click(object sender, EventArgs e)
{
    var itemIndex = listBox1.SelectedIndex;
    listBox1.Items.RemoveAt(itemIndex);
    listBox2.Items.RemoveAt(itemIndex);
    listBox3.Items.RemoveAt(itemIndex);
}

您可以将
SelectedIndex
RemoveAt
方法一起使用:

if (listBox1.SelectedIndex >= 0)
{
    listBox3.Items.RemoveAt(listBox1.SelectedIndex);
    listBox2.Items.RemoveAt(listBox1.SelectedIndex);
    listBox1.Items.RemoveAt(listBox1.SelectedIndex);
}

必须从listBox1中最后删除,因为您正在从该列表框读取索引,除非您将值复制到一个新变量中,如上面的示例所示。

必须从listBox1中最后删除,因为您正在从该列表框读取索引。
除非您将值复制到一个新变量中,如我的示例所示。