如何在vb.net中使用多个复选框进行计算?
例如,我有5个复选框,这些复选框是具有不同价格的产品的名称,这些名称是恒定的,并且在选中某个复选框时显示在五个文本框中 我还有5个文本框,用于数量,这取决于用户的输入。现在,例如,我只选中了2个复选框并输入它们的数量,比如说产品1=100和产品2=200,我为它们的两个数量输入2,只在一个文本框中显示总量。我如何编码它,因为当我使用if-else语句时,它只计算和显示一种产品的价格,而我选择或选中了两种产品;当我使用或在if语句中,当我只选中两个复选框并输入它们的数量时,我会得到一个错误,即格式字符串未处理。这是我的代码:如何在vb.net中使用多个复选框进行计算?,vb.net,Vb.net,例如,我有5个复选框,这些复选框是具有不同价格的产品的名称,这些名称是恒定的,并且在选中某个复选框时显示在五个文本框中 我还有5个文本框,用于数量,这取决于用户的输入。现在,例如,我只选中了2个复选框并输入它们的数量,比如说产品1=100和产品2=200,我为它们的两个数量输入2,只在一个文本框中显示总量。我如何编码它,因为当我使用if-else语句时,它只计算和显示一种产品的价格,而我选择或选中了两种产品;当我使用或在if语句中,当我只选中两个复选框并输入它们的数量时,我会得到一个错误,即格式
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
修复起来很简单,在一行中使用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