从两个列表框中删除选定的项目 [英] Delete selected item from two list boxes

查看:123
本文介绍了从两个列表框中删除选定的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个系统,一旦单击按钮,就会将食品添加到Listbox1,并将其价格添加到Listbox2.我正在尝试创建一个按钮,用户可以在其中删除食品,然后将其价格删除.

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)

推荐答案

大概这两个值在其相对的ListBox中都位于相同的索引处.鉴于您可以只计算索引并按索引删除两者

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)

这篇关于从两个列表框中删除选定的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆