# How to find the sum of multiple check boxes

• ### Question

• My code seems to be wrong, when i run the app the output just gives the number of the greatest checkbox...i need the app to give the total of all check boxes that i have selected.

Public Class Form1
Private Sub btnCheckout_Click(sender As Object, e As EventArgs) Handles btnCheckout.Click

'Declaring groups
Dim dblHoodies, dblShirts, dblTanks, dblFlags, dblTotal As Double

'Hoodies price
If Hoodie1.Checked Then
dblHoodies = 45.0
ElseIf Hoodie2.Checked Then
dblHoodies = 40.0
ElseIf Hoodie3.Checked Then
dblHoodies = 58.0

'Flags price
ElseIf Flag1.Checked Then
dblFlags = 30
ElseIf Flag2.Checked Then
dblFlags = 30
ElseIf Flag3.Checked Then
dblFlags = 35

'Tanks price
ElseIf Tank1.Checked Then
dblTanks = 10
ElseIf Tank2.Checked Then
dblTanks = 17
ElseIf Tank3.Checked Then
dblTanks = 10

'Shirts price
ElseIf Shirt1.Checked Then
dblShirts = 10
ElseIf Shirt2.Checked Then
dblShirts = 10
ElseIf Shirt3.Checked Then
dblShirts = 12
End If
'Checkout function, once checkout button is clicked

PictureBox1.Visible = True

dblTotal = dblShirts + dblTanks + dblFlags + dblHoodies

Const Format As String = "C2"
lblOutput.Text = "Your total is: " & dblTotal.ToString(Format) & ControlChars.NewLine

End Sub
PictureBox1.Visible = False
End Sub

Private Sub PBReset_Click(sender As Object, e As EventArgs) Handles PBReset.Click

'When reset button is hit all varibles of form will reset back to when the form was started

'Hoodie will uncheck
Hoodie1.Checked = False
Hoodie2.Checked = False
Hoodie3.Checked = False
'Flags will uncheck
Flag1.Checked = False
Flag2.Checked = False
Flag3.Checked = False

'Shirts will uncheck
Shirt1.Checked = False
Shirt2.Checked = False
Shirt3.Checked = False

'Tanks will uncheck
Tank1.Checked = False
Tank2.Checked = False
Tank3.Checked = False

PBReset.Visible = True
PictureBox1.Visible = False

End Sub
End Class

Wednesday, December 11, 2019 9:02 PM

### All replies

• Hi

Try this- some small but important changes made (BTW: if you want to allow user to select only 1 item per category, use RadioButtons)

```  Private Sub btnCheckout_Click(sender As Object, e As EventArgs) Handles btnCheckout.Click
'Declaring groups
Dim dblHoodies, dblShirts, dblTanks, dblFlags, dblTotal As Double

'Hoodies price
If Hoodie1.Checked Then
dblHoodies = 45.0
ElseIf Hoodie2.Checked Then
dblHoodies = 40.0
ElseIf Hoodie3.Checked Then
dblHoodies = 58.0
End If

'Flags price
If Flag1.Checked Then
dblFlags = 30
ElseIf Flag2.Checked Then
dblFlags = 30
ElseIf Flag3.Checked Then
dblFlags = 35
End If

'Tanks price
If Tank1.Checked Then
dblTanks = 10
ElseIf Tank2.Checked Then
dblTanks = 17
ElseIf Tank3.Checked Then
dblTanks = 10
End If

'Shirts price
If Shirt1.Checked Then
dblShirts = 10
ElseIf Shirt2.Checked Then
dblShirts = 10
ElseIf Shirt3.Checked Then
dblShirts = 12
End If
'Checkout function, once checkout button is clicked
dblTotal = dblShirts + dblTanks + dblFlags + dblHoodies

Const Format As String = "C2"
lblOutput.Text = "Your total is: " & dblTotal.ToString(Format) & ControlChars.NewLine
End Sub```

Regards Les, Livingston, Scotland

Wednesday, December 11, 2019 9:31 PM
• Hello,

There is a different approach to consider, keeping things simple, place RadioButton controls into GroupBoxes were each RadioButton .Tag is the price.

```Public Class Form1
Private hoodiePrice As Double
Private flagPrice As Double
Private Sub GetPricesButton_Click(sender As Object, e As EventArgs) _
Handles GetPricesButton.Click

FirstOrDefault(Function(rb) rb.Checked)

If hoodie IsNot Nothing Then
hoodiePrice = CDbl(hoodie.Tag)
End If
FirstOrDefault(Function(rb) rb.Checked)

If flag IsNot Nothing Then
flagPrice = CDbl(flag.Tag)
End If

MessageBox.Show(\$"Hoodie: {hoodiePrice} Flag: {flagPrice} Total: {hoodiePrice + flagPrice}")
End Sub

Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
End Sub
Dim AllGroupBoxes = Controls.OfType(Of GroupBox).ToList()

For Each groupBox In AllGroupBoxes
Dim gb = groupBox.Controls.OfType(Of RadioButton).Where(Function(rb) rb.Checked)
For Each rb In From rb1 In gb
rb.Checked = False
Next
Next
End Sub
Private Sub ResetButton_Click(sender As Object, e As EventArgs) Handles ResetButton.Click
End Sub
End Class
```
Option Strict On, Option Infer On

Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

NuGet BaseConnectionLibrary for database connections.

StackOverFlow

Wednesday, December 11, 2019 10:30 PM
• Hi,

If I understand correctly, please just change some of your "ElseIf" to "If" as Leshay's code shows and the cause is that you used the wrong "If" condition.

But one thing I don't understand is why you use CheckBox to do what you can do with RadioBoxe.

The advantage of using checkboxes is that multiple selection is possible, so it is better to use "If" instead of "ElseIf" as shown below:

```        'Hoodies price
If Hoodie1.Checked Then
dblHoodies = 45.0
End If
If Hoodie2.Checked Then
dblHoodies += 40.0
End If
If Hoodie3.Checked Then
dblHoodies += 58.0
End If```

Kareninstructor's reply is also a good choice for you.