例如我有5个复选框,这些复选框是具有不同价格的产品的名称,这些产品是常量,并在选中某个复选框时显示在五个文本框中 我还有5个文本框,数量取决于用户的输入。现在例如我只检查了2个复选框并输入了它们的数量,假设产品1 = 100,产品2 = 200,我输入2,它们的数量只显示一个文本框中的总金额。我怎么能编码呢,因为当我使用if else语句时,它只计算和显示一个产品的价格,而我选择或检查了2个产品,当我使用或在if语句中,当我只选中了2个复选框时输入他们的数量,我得到一个错误,格式字符串unhandle。这是我的代码:
If chkPopcorn.Checked = True Then
quantity1 = Integer.Parse(txtQuantityPopcorn.Text)
price1 = txtPricePopcorn.Text * quantity1
ElseIf chkBurger.Checked = True Then
quantity2 = Integer.Parse(txtQuantityBurger.Text)
price2 = txtPriceBurger.Text * quantity2
ElseIf chkSpaghetti.Checked = True Then
quantity3 = Integer.Parse(txtQuantitySpaghetti.Text)
price3 = txtPriceSpaghetti.Text * quantity3
ElseIf chkHotdog.Checked = True Then
quantity4 = Integer.Parse(txtQuantityHotdog.Text)
price4 = txtPriceHotdog.Text * quantity4
ElseIf chkCupcake.Checked = True Then
quantity5 = Integer.Parse(txtQuantityCupcake.Text)
price5 = txtPriceCupcake.Text * quantity5
End If
End If
txtSales.Text = price1 + price2 + price3 + price4 + price5
答案 0 :(得分:1)
简单修复,连续使用5个If语句,而不是ElseIf
If chkPopcorn.Checked = True Then
quantity1 = Integer.Parse(txtQuantityPopcorn.Text)
price1 = txtPricePopcorn.Text * quantity1
End If
If chkBurger.Checked = True Then
quantity2 = Integer.Parse(txtQuantityBurger.Text)
price2 = txtPriceBurger.Text * quantity2
End If
If chkSpaghetti.Checked = True Then
quantity3 = Integer.Parse(txtQuantitySpaghetti.Text)
price3 = txtPriceSpaghetti.Text * quantity3
End If
If chkHotdog.Checked = True Then
quantity4 = Integer.Parse(txtQuantityHotdog.Text)
price4 = txtPriceHotdog.Text * quantity4
End If
If chkCupcake.Checked = True Then
quantity5 = Integer.Parse(txtQuantityCupcake.Text)
price5 = txtPriceCupcake.Text * quantity5
End If
txtSales.Text = price1 + price2 + price3 + price4 + price5
End Sub