我想从列表框中将它们添加到组合框后,计算项目的总价格。在列表框中,我有两种类型的物品和ts价格。我希望看到总价格增加,因为我将每个项目(单击addButton)添加到组合框。但是我看到的是该项目被添加到组合框,但我只看到单个项目价格而不是价格总和。这是我的代码示例。如何使用for循环来总结项目
private void addButton_Click(object sender, EventArgs e)
{
decimal price; // variables to holds the price
decimal total = 0; // variables to hold the total
int counter;
for (counter=0; counter <= 5; counter++)
{
price = decimal.Parse(priceLabel2.Text);
// add items price
total += price;
// display the total amount
costLabel.Text = total.ToString("c");
}
任何帮助,将不胜感激,
如果添加4个项目,显示的总数(当前)是否只添加最后一个? – mjwills
是的,它只显示最后一个。 –
如果您发布的代码与您提供的描述相关,那么这将更容易。你解释一下列表框和组合框,然后发布处理一些标签的代码。另外,请修复缩进。 –