1

Here's the image of my design interface:Design Interface

I want the user to make a selection and enter the quantity they wish to purchase then once they are done and press calculate, it gives the total of the items selected according to the quantity entered and inputs that total in the price textboxes.

Here's my code... I am stuck...

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 Answers1

1

On the button click you will have to look after the checkboxes that are checked and then do the proper math

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
Turbo
  • 61
  • 4
  • Mine is a checked list box not a check box therefore 'If MangoChk.IsChecked Then' will not apply for my code –  Nov 06 '18 at 12:25
  • insted of "If MangoChk.IsChecked Then" use your code, "If Me.chkJuices.Items().ToString = "Mango"" – Turbo Nov 06 '18 at 12:32
  • when I run my code it brings an error if I have not selected all items on the page –  Nov 06 '18 at 12:39
  • On button click or on load? what does the error say? – Turbo Nov 06 '18 at 12:44
  • System.InvalidCastException: 'Conversion from string "" to type 'Integer' is not valid.' This happens when I miss to select even one item. –  Nov 06 '18 at 12:47
  • Also, when I enter a value on the quantity textbox and then calculate, it outputs the total as 0. –  Nov 06 '18 at 12:53
  • https://learn.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-determine-checked-items-in-the-windows-forms-checkedlistbox-control i would use the second example of this webpage, get the name of the checked items and sum – Turbo Nov 06 '18 at 13:10
  • Edited my answer... and by the way, before saving text to a integer value, check if the text is not empty, i think thats why you are getting those error. for example: if txtMango.Text.Trim <> "" Then tMango = txtMango.Text Else 0 AND MAKE SURE its numbers only, here: https://stackoverflow.com/questions/463299/how-do-i-make-a-textbox-that-only-accepts-numbers – Turbo Nov 06 '18 at 13:21
  • I appreciate this. Thank you. –  Nov 06 '18 at 13:35
  • Glad i could help you – Turbo Nov 06 '18 at 13:55