user3212628
user3212628

Reputation: 37

Delete selected item from two list boxes

I have a system where once a button is clicked a food item is added to Listbox1 and its price is added to Listbox2. I'm trying to create a button where the user can delete the food item and its price will be deleted.

I'm very new to this but I gave it a go at trying to achieve this task with my code below. The item deletes fine but the first price in the list is deleted, not the price of the item. This is a problem when there are multiple items. The program also crashes if there are no prices

If anyone could help that would be great, cheers

 Dim itemdel As Integer

    itemdel = ListBox1.SelectedValue

    ListBox2.Items.RemoveAt(itemdel)

    ListBox1.Items.Remove(ListBox1.SelectedItem)

Upvotes: 1

Views: 1887

Answers (2)

Farag Alargt
Farag Alargt

Reputation: 86

I have the same problem, this solution don't work to me. my form contain two listboxs listbox1 contain names. listbox4 contain ids. i want select name to delete it and immediately id deleted from listbox4. after many training. this solution work to me.

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

sorry for my weak English skill.

Upvotes: 0

JaredPar
JaredPar

Reputation: 754575

Presumably both of these values exist at the same index within their relative ListBox. Given that you can just calculate the index and remove both by index

Dim index As Integer = ListBox1.SelectedIndex
ListBox1.Items.RemoveAt(index)
ListBox2.Items.RemoveAt(index)

Upvotes: 2

Related Questions