how to calculate total of selected combo box items

richardmagaisa 0 Tallied Votes 114 Views Share
If Me.cbobraketype.SelectedItem = ("Rim Brakes") Then
        braketype = ("Rim Brakes")
        price = "£120"
        braketype = ("Rim Brakes")


    ElseIf Me.cbobraketype.SelectedItem = ("Disk Brakes") Then
        braketype = ("Disk Brakes")
        price = "£150"
        braketype = ("Disk Brakes")



    End If

    'choose type of frame
    If Me.cboframetype.SelectedItem = ("Aluminium") Then
        frametype = ("Aluminium")

    End If

    'choose type of brakes
    If Me.cbobraketype.SelectedItem = ("Rim Brakes") Then
        braketype = ("Rim Brakes")
        price = "£390"
        braketype = ("Rim Brakes")

    ElseIf Me.cbobraketype.SelectedItem = ("Disk Brakes") Then
        braketype = ("Disk Brakes")
        price = "£430"
        braketype = ("Disk Brakes")

    End If
If Me.cbobraketype.SelectedItem = ("Rim Brakes") Then
            braketype = ("Rim Brakes")
            price = "£120"
            braketype = ("Rim Brakes")


        ElseIf Me.cbobraketype.SelectedItem = ("Disk Brakes") Then
            braketype = ("Disk Brakes")
            price = "£150"
            braketype = ("Disk Brakes")



        End If

        'choose type of frame
        If Me.cboframetype.SelectedItem = ("Aluminium") Then
            frametype = ("Aluminium")

        End If

        'choose type of brakes
        If Me.cbobraketype.SelectedItem = ("Rim Brakes") Then
            braketype = ("Rim Brakes")
            price = "£390"
            braketype = ("Rim Brakes")

        ElseIf Me.cbobraketype.SelectedItem = ("Disk Brakes") Then
            braketype = ("Disk Brakes")
            price = "£430"
            braketype = ("Disk Brakes")

        End If

Dani AI

Generated

The snippet posted by shows two common problems: repeating the same conditional logic in multiple places (which causes later assignments to silently overwrite earlier ones) and storing prices as text with a currency symbol. Storing money as strings prevents arithmetic, and comparing SelectedItem directly to literals can fail if items are objects or the ComboBox is data-bound. Prefer keeping numeric prices as Decimal and binding or associating the price with each item so totals can be summed reliably.

A compact, maintainable pattern is to bind a list of (display, price) pairs and use SelectedValue for arithmetic. The sample below uses Decimal for prices and formats the total only when showing it:

' Bind (e.g. in Form.Load)
Dim brakeOptions = New List(Of KeyValuePair(Of String, Decimal)) From {
    New KeyValuePair(Of String, Decimal)("Standard Brake", 100D),
    New KeyValuePair(Of String, Decimal)("Premium Brake", 140D)
}
cbBrake.DataSource = New BindingSource(brakeOptions, Nothing)
cbBrake.DisplayMember = "Key"
cbBrake.ValueMember = "Value"

' Sum selected prices
Private Function SelectedPrice(cbo As ComboBox) As Decimal
    If cbo.SelectedValue Is Nothing Then Return 0D
    Return Convert.ToDecimal(cbo.SelectedValue)
End Function

Private Sub UpdateTotal()
    Dim total As Decimal = SelectedPrice(cbBrake) + SelectedPrice(cbFrame)
    lblTotal.Text = total.ToString("C", System.Globalization.CultureInfo.CurrentCulture)
End Sub

If data-binding is not desired, use a small item class (Text + Price) and override ToString so the ComboBox shows the name while the price is retrievable:

Private Class ComboItem
    Public Property Text As String
    Public Property Price As Decimal
    Public Overrides Function ToString() As String
        Return Text
    End Function
End Class
' Add items and read selected price via TryCast(SelectedItem, ComboItem).Price

Troubleshooting notes: always use Decimal for money, avoid embedding currency symbols in stored values, guard against null SelectedValue, and call the total-calculation from selection-changed handlers. Consolidating mapping (dictionary, binding list, or item class) removes duplicated If/Else blocks and makes the sum straightforward and reliable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.