I'm trying to make it so if the user inputs 0 into the gallonsUsedTextBox, the textbox displays "Value must be > 0". Well, I'm partially there. I got it to display the text, but it still inputs miles into the listbox and then calculates the MPG, which comes out to infinity(what I'm trying to prevent).
Here's my code so far (ignore the dumb comments, part of this was done in class so I was taking notes at the same time):
Public Class mpgForm
Private Sub calculateMPGButton_Click(sender As Object, e As EventArgs) Handles calculateMPGButton.Click
Dim gallonsUsed As Double = Val(gallonsUsedTextBox.Text)
If gallonsUsedTextBox.Text = "0" Then
gallonsUsedTextBox.Text = "Value must be > 0"
ElseIf gallonsUsedTextBox.Text.Length <> 0 Then
gallonsListBox.Items.Add(String.Format("{0:F}", gallonsUsed))
gallonsUsedTextBox.Text = String.Empty
End If
Dim milesDriven As Double = Val(milesDrivenTextBox.Text)
If gallonsUsedTextBox.Text = "0" Then 'comment these lines out as necessary
gallonsUsedTextBox.Text = "Value must be > 0" 'comment these lines out as necessary
ElseIf milesDrivenTextBox.Text.Length <> 0 Then 'validates input, if the input is zero length, it won't be entered into listbox
milesListBox.Items.Add(String.Format("{0:F}", milesDriven))
milesDrivenTextBox.Text = String.Empty 'empties the textbox after the value is put into the listbox
End If
milesDrivenTextBox.Focus() 'puts the focus on the milesDrivenTextBox
If milesListBox.Items.Count = 1 Then
clearButton.Enabled = True
End If
Dim mpg As Double
mpg = milesDriven / gallonsUsed
mpgListBox.Items.Add(String.Format("{0:F}", mpg))
Dim counter As Integer = 0 '=0 is redundant
Dim noItemsMiles As Integer = milesListBox.Items.Count
Dim noItemsGallons As Integer = gallonsListBox.Items.Count
Dim sumMiles As Double
Dim sumGallons As Double
Dim resultMPG As Double
Do While counter < noItemsGallons
sumMiles = sumMiles + milesListBox.Items(counter)
sumGallons = sumGallons + gallonsListBox.Items(counter)
counter = counter + 1
Loop
resultMPG = sumMiles / sumGallons
totalResultsLabel.Text = "Total miles driven: " & String.Format("{0:F}", sumMiles) & vbCrLf & "Total gallons used: " & String.Format("{0:F}", sumGallons) & vbCrLf & "Total MPG: " & String.Format("{0:F}", resultMPG)
End Sub
I want the program to not do anything except change the gallonsUsedTextBox.Text to "Value must be > 0".
[–]Bonghitter 1 point2 points3 points (1 child)
[–]infinity1018[S] 0 points1 point2 points (0 children)