【发布时间】:2015-12-01 17:07:56
【问题描述】:
我正在尝试创建一种方法,该方法将采用一个列表框的值,并且还将从同一索引处的另一个列表框中取出。我只是 C# 的初学者,这就是我遇到这个问题的原因。在此先感谢您的帮助
if (lstCheckoutProduct.)
{
lstCheckoutProduct.Items.Remove(lstCheckoutProduct.SelectedItem);
int productIndex = lstCheckoutProduct.Items.IndexOf(lstCheckoutProduct.SelectedIndex);
lstCheckoutPrice.Items.Remove(productIndex);
}
else
{
lstCheckoutPrice.Items.Remove(lstCheckoutPrice.SelectedItem);
int priceIndex = lstCheckoutPrice.Items.IndexOf(lstCheckoutPrice.SelectedIndex);
lstCheckoutPrice.Items.Remove(priceIndex);
}
【问题讨论】:
-
您的第一个 if 语句是否应该检查是否有选定的项目?它只是以一个点结束
-
lstCheckoutPrice.Items.IndexOf(lstCheckoutPrice.SelectedIndex)为什么? -
在你的 else 我认为你应该从 lstCheckoutProduct.items 而不是 lstCheckoutPrice.items 中删除
-
我使用了选定的索引,因为我认为如果我获得产品的位置,那么价格将在不同的列表框上的相同索引中,因此我可以找到它
-
@M.Walker,您无需同时尝试
IndexOf和SelectedIndex。SelectedIndex就够了。
标签: c# visual-studio listbox listboxitems