如何从选中的列表框中获取所选项目并计算其总数

时间:2018-11-06 11:28:35

标签: vb.net visual-studio checkedlistbox

这是我的设计界面的图像:Design Interface

我希望用户进行选择,然后输入要购买的数量,然后完成计算并按计算,它会根据输入的数量给出所选项目的总数,并在价格文本框中输入总数。

这是我的代码... 我被困住了...

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
    Me.Close()

End Sub

Public Sub frmDrinks_Load(sender As Object, e As EventArgs) Handles MyBase.Load


End Sub

Public Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    Dim tMango As Integer
    Dim tOrange As Integer
    Dim tStrawberry As Integer
    Dim tWatermelon As Integer
    Dim tMinty As Integer
    Dim tCoke As Integer
    Dim tDiet As Integer
    Dim tSprite As Integer
    Dim tMineral As Integer
    Dim tSpark As Integer
    Dim tManSmooth As Integer
    Dim tBanSmooth As Integer
    Dim tTropSmooth As Integer
    Dim tStrawSmooth As Integer
    Dim tVanilla As Integer
    Dim tCookie As Integer
    Dim tManShake As Integer
    Dim tBanShake As Integer


    tMango = txtMango.Text
    tOrange = txtOrange.Text
    tStrawberry = txtStrawberry.Text
    tWatermelon = txtWatermelon.Text
    tMinty = txtMinty.Text
    tCoke = txtCoke.Text
    tDiet = txtDiet.Text
    tSprite = txtSprite.Text
    tMineral = txtMineral.Text
    tSpark = txtSparkling.Text
    tManSmooth = txtMansmooth.Text
    tBanSmooth = txtBanana.Text
    tTropSmooth = txtTropical.Text
    tStrawSmooth = txtStrawSmooth.Text
    tVanilla = txtVanilla.Text
    tCookie = txtChipCookie.Text
    tManShake = txtManShake.Text
    tBanShake = txtBanShake.Text


    Dim TotalSum As Double = 0
    If Me.chkJuices.Items().ToString = "Mango" Then
        TotalSum += (100 * tMango)
    End If
    If Me.chkJuices.Items().ToString = "Orange" Then
        TotalSum += (100 * tOrange)
    End If
    If Me.chkJuices.Items().ToString = "Strawberry" Then
        TotalSum += (100 * tStrawberry)
    End If
    If Me.chkJuices.Items().ToString = "Watermelonade" Then
        TotalSum += (100 * tWatermelon)
    End If
    If Me.chkJuices.Items().ToString = "Minty Pineade" Then
        TotalSum += (100 * tMinty)
    End If

    txtJuice.Text = TotalSum
End Sub

1 个答案:

答案 0 :(得分:1)

在按钮上单击,您将需要照管选中的复选框,然后进行正确的数学运算

Private Sub Calculate() Handles Button.click
    Dim TotalSum As Double = 0
    For i = 0 To (CheckedListBox1.Items.Count - 1)
        If CheckedListBox1.GetItemChecked(i) = True Then
            If CheckedListBox1.Items(i).ToString = "Mango" Then
                TotalSum += (100 * tMango)
            End If

            If CheckedListBox1.Items(i).ToString = "Orange" Then
                TotalSum += (100 * tOrange)
            End If
        End If
    Next
End Sub