从两个列表框中删除所选项目

时间:2014-02-14 18:52:39

标签: vb.net listbox

我有一个系统,一旦点击一个按钮,食物项目就被添加到Listbox1,它的价格被添加到Listbox2。我试图创建一个用户可以删除食品的按钮,其价格将被删除。

我对此非常陌生,但我尝试使用下面的代码尝试完成此任务。该项目删除正常,但删除列表中的第一个价格,而不是项目的价格。当有多个项目时,这是一个问题。如果没有价格,程序也会崩溃

如果有人可以提供帮助那就太棒了,欢呼声

 Dim itemdel As Integer

    itemdel = ListBox1.SelectedValue

    ListBox2.Items.RemoveAt(itemdel)

    ListBox1.Items.Remove(ListBox1.SelectedItem)

2 个答案:

答案 0 :(得分:1)

据推测,这两个值都存在于其相对ListBox内的相同索引处。鉴于您可以只计算索引并通过索引

删除它们
Dim index As Integer = ListBox1.SelectedIndex
ListBox1.Items.RemoveAt(index)
ListBox2.Items.RemoveAt(index)

答案 1 :(得分:0)

我有同样的问题,这种解决方案对我不起作用。我的表单包含两个列表框 listbox1包含名称。 listbox4包含ID。 我想选择名称将其删除,并立即从listbox4中删除ID。 经过多次培训。这个解决方案对我有用。

Dim delname As String = ListBox1.SelectedItem
Dim delid As Integer = ListBox1.SelectedIndex
If ListBox1.Items.Count <> -1 Then
If ListBox1.SelectedIndex <> -1 Then
ListBox4.Items.RemoveAt(delid)
ListBox1.Items.Remove(delname)
End If
End If

对不起,我的英语能力很弱。

相关问题