Does anyone know how to sum up all the numbers in a list and display the total i
ID: 3530922 • Letter: D
Question
Does anyone know how to sum up all the numbers in a list and display the total in a label? I am having trouble with the code below.. Public Class Form1 Private Sub btnAddWorkshop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddWorkshop.Click Dim dblDay, dblFee, dblStay, dblMacCount, dblTotal As Double Dim strTotal As String 'Workshop selection Dim strWorkshop As String = lstWorkshop.SelectedIndex() If lstWorkshop.SelectedItem = "Handling Stress" Then dblDay = 3 dblFee = 595 ElseIf lstWorkshop.SelectedItem = "Time Management" Then dblDay = 3 dblFee = 695 ElseIf lstWorkshop.SelectedItem = "Supervision Skills" Then dblDay = 3 dblFee = 995 ElseIf lstWorkshop.SelectedItem = "Negotiation" Then dblDay = 5 dblFee = 1295 ElseIf lstWorkshop.SelectedItem = "How to Interview" Then dblDay = 1 dblFee = 395 ElseIf lstWorkshop.SelectedItem Is Nothing Then MessageBox.Show("Plese select a Workshop first") End If 'Location selection Dim location As String = lstLocation.SelectedIndex() If lstLocation.SelectedItem = "Austin" Then dblStay = dblDay * 95 ElseIf lstLocation.SelectedItem = "Chicago" Then dblStay = dblDay * 125 ElseIf lstLocation.SelectedItem = "Dallas" Then dblStay = dblDay * 110 ElseIf lstLocation.SelectedItem = "Orlando" Then dblStay = dblDay * 100 ElseIf lstLocation.SelectedItem = "Phoenix" Then dblStay = dblDay * 92 ElseIf lstLocation.SelectedItem = "Raleigh" Then dblStay = dblDay * 90 End If 'Total cost dblTotal = dblStay + dblFee strTotal = dblTotal.ToString("c") 'Display in list box lstCosts.Items.Add(strTotal) End Sub Private Sub btnTotal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTotal.Click End Sub Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click 'reset everything...... lstCosts.Items.Clear() lstLocation.Items.Clear() lstWorkshop.Items.Clear() lblTotal.Text = " " 'Reset the focus..... lstWorkshop.Focus() End Sub Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click Me.Close() End Sub End ClassExplanation / Answer
Public Class Form1
Dim dblFinalCost As Double
Private Sub btnAddWorkshop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddWorkshop.Click
Dim dblRegFee, dblDays, dblLocFee, dblTotalCost As Double
If lstWorkshops.SelectedIndex = -1 Then
MessageBox.Show("Select a workshop.")
ElseIf lstLocations.SelectedIndex = -1 Then
MessageBox.Show("Select a location.")
Else
Select Case lstWorkshops.SelectedIndex
Case 0
dblRegFee = 595
dblDays = 3
Case 1
dblRegFee = 695
dblDays = 3
Case 2
dblRegFee = 995
dblDays = 3
Case 3
dblRegFee = 1295
dblDays = 5
Case 4
dblRegFee = 395
dblDays = 1
End Select
Select Case lstLocations.SelectedIndex
Case 0
dblLocFee = 95
Case 1
dblLocFee = 125
Case 2
dblLocFee = 110
Case 3
dblLocFee = 100
Case 4
dblLocFee = 92
Case 5
dblLocFee = 90
End Select
dblTotalCost = dblRegFee + (dblLocFee * dblDays)
lstCosts.Items.Add(dblTotalCost)
End If
End Sub
Private Sub btnCalcTotal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalcTotal.Click
If lstCosts.Items.Count > 0 Then
For intCount As Integer = 0 To lstCosts.Items.Count - 1
dblFinalCost = dblFinalCost + Val(lstCosts.Items.Item(intCount))
Next
Else
MessageBox.Show("Something has gone horribly, horribly wrong.")
End If
txtTotalCost.Text = dblFinalCost.ToString("c")
End Sub
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
lstWorkshops.SelectedIndex = -1
lstWorkshops.SelectedIndex = -1
lstCosts.Items.Clear()
txtTotalCost.Clear()
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.