【发布时间】:2016-12-04 11:14:56
【问题描述】:
我有一个简单的应用程序,其中将值添加到列表框中,并且作为双精度的价格位于作为字符串的项目的右侧。我编写了一个标签代码,每次将一个项目添加到列表框时,它都会将价格加起来。从列表框中删除选定项时如何更新标签。
public void Form2_Load(object sender, EventArgs e)
{
foreach (Control butt in groupBox2.Controls)
{
if (butt is Button)
{
((Button)butt).Click += Form2_Click;
}
}
}
public void Form2_Click(object sender, EventArgs e)
{
string st1 = ((Button)sender).Text;
if (st1 == "Family_Pizza")
{
price = 22.95;
}
else if (st1 == "Large_Pizza")
{
price = 16.95;
}
else if (st1 == "Medium Pizza")
{
price = 11.95;
}
else if (st1 == "Small Pizza")
{
price = 7.95;
}
else if (st1 == "Garlic Bread")
{
price = 4.95;
}
else if (st1 == "BBQ Ribs")
{
price = 9.95;
}
else if (st1 == "BBQ Wings")
{
price = 9.95;
}
else if (st1 == "Express Combo")
{
price = 5.95;
}
else if (st1 == "1.25 L Drink")
{
price = 4.50;
}
else if (st1 == "375 ml Drink")
{
price = 2.60;
}
else if (st1 == "600 ml Drink")
{
price = 3.50;
}
else if (st1 == "Ben 'n' Jerry Core")
{
price = 13.50;
}
else if (st1 == "Ben 'n' Jerry Pint")
{
price = 11.95;
}
s2 = price.ToString("C");
string item = st1;
string value=s2.PadLeft(s2.Length+8,' ');
listBox1.Items.Add(item+value);
updprice = price + updprice;
label2.Text = updprice.ToString("C");
}
private void button3_Click(object sender, EventArgs e)
{
listBox1.Items.Remove(listBox1.SelectedItem);
}
【问题讨论】:
-
请编辑此问题并发布您拥有的所有相关代码
}而不打开{如果这是在方法内部显示完整方法并向我们指出您的代码中的内容没有按照你期望的方式发生.. -
放上更新
label值的完整代码。 -
现在检查代码。谢谢