这是我在Stackoverflow上的第一篇文章,请善良:D!
我正在为课堂开发餐厅账单计算器。给定四个组合的用户输入:饮料,开胃菜,主菜&甜点,我的代码计算了小计,税收和总计。对于每个Commobox,我都有一个与每个项目关联的预定义项目和值的列表。我已经能够成功地编码。当用户选择他们的项目时,我的小计,提示&总文本框会自动更新。subtotal = bevprice appprice mainprice dessprice。税=小计 * .1m。总计=小计 税。
我的问题:说用户对每个类别(饮料,应用,主,甜点)的选择输入。然后,假设用户回到饮料(Combobox1),并决定将其从一个选项更改为另一种选项。即使选择了一个选项,我的代码零即使选择了其他选择。同样,说用户输入所有四个选择,然后返回更改其主课程(CompoBox3),我的代码保存了ComboBox1&2(饮料& appetizer)在新选择的主课程中添加,但零combobox4(甜点)。
我知道由于选择Indexchanged活动处理程序而出现了这个问题。如果ComboBox2的选择更改,则计算Combobox 1&2,但是ComboBox3&4因此,它将其恢复为0值。对吗?
我确定答案很明显,但是我是新手。谢谢!
无论如何,这是我的代码的预览(对不起,我很渴望):
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Menu bevSelect = comboBox1.SelectedItem as Menu;
textBox3.Text = Convert.ToString(bevSelect.bevPrice);
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
Menu appSelect = comboBox2.SelectedItem as Menu;
Menu bevSelect = comboBox1.SelectedItem as Menu;
textBox3.Text = Convert.ToString(bevSelect.bevPrice + appSelect.appPrice);
}
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
Menu mainSelect = comboBox3.SelectedItem as Menu;
Menu appSelect = comboBox2.SelectedItem as Menu;
Menu bevSelect = comboBox1.SelectedItem as Menu;
textBox3.Text = Convert.ToString(bevSelect.bevPrice + appSelect.appPrice + mainSelect.mainPrice);
}
private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
{
Menu dessSelect = comboBox4.SelectedItem as Menu;
Menu mainSelect = comboBox3.SelectedItem as Menu;
Menu appSelect = comboBox2.SelectedItem as Menu;
Menu bevSelect = comboBox1.SelectedItem as Menu;
textBox3.Text = Convert.ToString(bevSelect.bevPrice + appSelect.appPrice + mainSelect.mainPrice + dessSelect.dessPrice);
这是一个视频!
发生这种情况,因为在第一个处理程序上,您仅在第一个组合框中填写文本框。您应该根据所有4个选定的项目来制作一个函数来计算账单,并应在SelectedIndexchanged处理程序中调用它。基本上您的方法应该做您上一个处理程序的工作。
private void comboBox1/2/3/4_SelectedIndexChanged(object sender, EventArgs e)
{
calculateBill();
}
private void calculateBill()
{
Menu dessSelect = comboBox4.SelectedItem as Menu;
Menu mainSelect = comboBox3.SelectedItem as Menu;
Menu appSelect = comboBox2.SelectedItem as Menu;
Menu bevSelect = comboBox1.SelectedItem as Menu;
textBox3.Text = Convert.ToString(bevSelect.bevPrice + appSelect.appPrice + mainSelect.mainPrice + dessSelect.dessPrice);
}