【问题标题】:how to do calculations with multiple check boxes in vb.net?如何在 vb.net 中使用多个复选框进行计算?
【发布时间】:2014-08-04 17:57:31
【问题描述】:

例如,我有5个复选框,这些复选框是具有不同价格的产品的名称,这些产品是恒定的,并且在选中某个复选框时显示在五个文本框中 我也有 5 个文本框用于数量取决于用户的输入。现在例如,我只检查了 2 个复选框并输入了它们的数量,比如说产品一 = 100 和产品 2 = 到 200,我为它们的两个数量输入 2,仅在一个文本框中显示总金额。我该如何编码,因为当我使用 if else 语句时,它只计算并显示一种产品的价格,而我选择或检查了 2 种产品,当我使用或在 if 语句中,当我只选中 2 个复选框和输入它们的数量,我得到一个错误,即格式字符串未处理。这是我的代码:

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

【问题讨论】:

    标签: vb.net


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-03
      • 1970-01-01
      • 1970-01-01
      • 2013-12-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多