Reputation: 7
So I'm trying to make a button that removes a value from a listbox but i have a method that removes with the value that it has stored.
How do i take the value of selected item and put it in the method?
private void putBack_btn_Click(object sender, EventArgs e)
{
string text = shoppingKart_listBox.GetItemText(shoppingKart_listBox.SelectedItem);
for (int n = shoppingKart_listBox.Items.Count - 1; n >= 0; --n)
{
if (shoppingKart_listBox.Items[n].ToString().Contains(text))
{
shoppingKart_listBox.Items.RemoveAt(n);
shopping.putBack(text) // ??
}
}
}
and the method itself
public void putBack(Item itemToPutBack)
{
amountLeft += itemToPutBack.price;
items.Remove(itemToPutBack);
}
Upvotes: 1
Views: 103
Reputation: 12171
Store reference before deleting item from list:
if (shoppingKart_listBox.Items[n].ToString().Contains(text))
{
var item = shoppingKart_listBox.Items[n];
shoppingKart_listBox.Items.RemoveAt(n);
shopping.putBack(item);
}
Upvotes: 1