Windows窗体ListView - 如何获取所选项的值

时间:2016-08-24 08:11:04

标签: c# winforms windows-forms-designer

我目前正在尝试为购物篮创建一个应用程序,您可以在其中输入一个项目,然后在两个方框中输入价格。在点击添加按钮时,项目被添加到列表视图。您可以多次执行此操作以向购物篮添加更多商品。

如果您决定不想在购物篮中放置商品,我会有一个删除按钮,用于删除您通过单击选择的商品。我的问题是,无论您删除哪个项目,您添加的最后一项的值都取自Total而不是正确的值。

我在此处添加项目的代码:

decimal total = numberPicker.Value * priceSetter.Value;
MessageBox.Show("You want " + numberPicker.Value + " " + txtName.Text + " which cost £" + priceSetter.Value + " each. Your Total cost is: £" + total, "My App");
ShoppingCart.Items.Add("Item: " + txtName.Text + "     " + 
                       "Value: " + priceSetter.Value + "     " +
                       "Quantity: " + numberPicker.Value + "     " +
                       "Total: " + (priceSetter.Value * numberPicker.Value)).ToString();
runningTotal = runningTotal + total;
resultBox.Text = runningTotal.ToString();
File.AppendAllText(@"C:\ShoppingCartExperiment\Reciept.txt", ("Item: " + txtName.Text + " " + "Price: £" + priceSetter.Value + " " + "Quantity: " + numberPicker.Value + " " + " Running Total: £" + runningTotal + Environment.NewLine));

我的移除代码在这里:

var selectedItem = ShoppingCart.SelectedItem;
ShoppingCart.Items.Remove(selectedItem);
decimal total = numberPicker.Value * priceSetter.Value;
runningTotal = runningTotal - total;
var SelectedTotal = (priceSetter.Value * numberPicker.Value);
File.AppendAllText(@"C:\ShoppingCartExperiment\Reciept.txt", ("You removed " + txtName.Text + Environment.NewLine));
MessageBox.Show(txtName.Text + " has been removed from basket");
File.AppendAllText(@"C:\ShoppingCartExperiment\Reciept.txt", ("You new total is: £" + runningTotal + Environment.NewLine));

我的项目很小,所以这两段代码几乎就是我所拥有的。 我的想法是每当我添加一个新项目时,我的priceSetter和NumberPicker(数量)都被过度使用。选择项目似乎不会存储我选择的项目的值,而只会存储我最后添加的项目的值。

我很擅长编码,请原谅任何Naivety。所有帮助都是赞赏的。

0 个答案:

没有答案