this is my first time posting on here. I'm having some difficulty with a program for a class I'm taking. I need to get the miles and gallons then calculate the MPG for each entry. I have this part figured out. What I can't seem to get is the totals at the bottom. I need to add the sum of all the items in each ListBox and calculate the total MPG each time the button is clicked. Here's my code so far.
Public Class MilesPerGallon
Private Sub calculateMPGButton_Click(sender As System.Object,
ByVal e As System.EventArgs) Handles calculateMPGButton.Click
Dim miles As Double ' miles driven
Dim gallons As Double ' gallons used
Dim totalMiles As Double ' total miles driven
Dim totalGallons As Double ' total gallons used
Dim counter As Integer
miles = milesDrivenTextBox.Text ' get the miles driven
gallons = gallonsUsedTextBox.Text ' get the gallons used
If milesDrivenTextBox.Text <> String.Empty Then
' add miles to the end of the milesListBox
milesListBox.Items.Add(milesDrivenTextBox.Text)
milesDrivenTextBox.Clear() ' clears the milesDrivenTextBox
End If
If gallonsUsedTextBox.Text = 0 Then
' do not divide by 0 and alert 0
gallonsUsedTextBox.Text = "Cannot equal 0"
End If
If gallonsUsedTextBox.Text > 0 Then
' add gallons to the end of the gallonsListBox
gallonsListBox.Items.Add(gallonsUsedTextBox.Text)
mpgListBox.Items.Add(String.Format("{0:F}", miles / gallons))
gallonsUsedTextBox.Clear() ' clears the gallonsUsedTextBox
End If
totalMiles = 0
totalGallons = 0
counter = 0
Do While totalMiles < milesListBox.Items.Count
miles = milesListBox.Items(totalMiles)
totalMiles += miles
counter += 1
Do While totalGallons < gallonsListBox.Items.Count
gallons = gallonsListBox.Items(totalGallons)
totalGallons += gallons
counter += 1
Loop
Loop
If totalMiles <> 0 Then
totalResultsLabel.Text = "Total miles driven: " & totalMiles & vbCrLf &
"Total gallons used: " & totalGallons & vbCrLf & "Total MPG: " &
String.Format("{0:F}", totalMiles / totalGallons)
End If
End Sub
End Class
Thanks in advance for any help you can provide.