patternMinor
Userform that calculates gas mileage
Viewed 0 times
gasuserformmileagethatcalculates
Problem
I created a project for a programming class that asks you to make a userform in Visual Studio that calculates mileage, given the user inputs of gallons of gas consumed and distance traveled. The code does its job fine, but I can't help but feel that my use of Do...While and Try...Catch statements are less than elegant. If anyone has suggestions or tips or cryptic hints about what would improve my code, I would be grateful!
The sub I use to populate the form follows:
```
Priv
Private Sub btnMileage_Click(sender As Object, e As EventArgs) Handles btnMileage.Click
Dim sngGas As Single, sngDistance As Single, sngMileage As Single
Do While sngGas <= 0 'force user to input positive number for gallons of gas consumed
Try 'reject all inputs that are not real numbers
sngGas = CSng(InputBox("Please enter gasoline consumed in gallons:"))
If sngGas <= 0 Then 'remind user to input number greater than 0
MsgBox("Please make sure your input is greater than 0.")
End If
Catch ex As Exception
MsgBox("Error: Please enter a valid number.") 'remind user that all inputs that aren't numbers are rejected
End Try
Loop
Do While sngDistance <= 0
Try 'reject all inputs that are not real numbers
sngDistance = CSng(InputBox("Please enter distance travelled in miles:")) 'force user to enter positive value for distance
If sngDistance <= 0 Then
MsgBox("Please make sure your input is greater than 0.")
End If
Catch ex As Exception
MsgBox("Error: Please enter a valid number.") 'reminder user that all inputs that aren't numbers are rejected
End Try
Loop
sngMileage = Math.Round((sngDistance / sngGas), 1, MidpointRounding.AwayFromZero) 'calculate mileage
PopulateForm(CStr(sngGas), CStr(sngDistance), CStr(sngMileage)) 'update form with gas, distance & mileage
MsgBox("All done!")
End SubThe sub I use to populate the form follows:
```
Priv
Solution
You have a form -- by definition, a window in which text boxes and such can appear. Why are you using
If you instead put elements on the form and used those (particularly
Watch this. Create a form, put three elements on it -- two
Then, inside the form class, put something like:
Aaaaand done.
That's the entirety of the code you have to write to make the form work. The controls take care of ensuring that the user inputs a number. You can even set the allowable range of values, so you don't have to care about negative numbers. You can add some code to, say, round the output, if you want.
MsgBox and InputBox to communicate with the user?If you instead put elements on the form and used those (particularly
NumericUpDown), the code part would be so much simpler -- and far more like, well, a Windows app.Watch this. Create a form, put three elements on it -- two
NumericUpDown controls named GasAmountBox and DistanceBox, and one text control (text box or label, your choice really) named MileageOutput. Add other elements (like labels for the controls) to suit your taste.Then, inside the form class, put something like:
Private Sub InputsChanged(sender As Object, e As EventArgs) _
Handles GasAmountBox.ValueChanged, DistanceBox.ValueChanged
If (GasAmountBox.Value = 0) Then
MileageOutput.Text = "---"
Else
Dim mileage = DistanceBox.Value / GasAmountBox.Value
MileageOutput.Text = mileage.ToString() & " MPG"
End If
End SubAaaaand done.
That's the entirety of the code you have to write to make the form work. The controls take care of ensuring that the user inputs a number. You can even set the allowable range of values, so you don't have to care about negative numbers. You can add some code to, say, round the output, if you want.
Code Snippets
Private Sub InputsChanged(sender As Object, e As EventArgs) _
Handles GasAmountBox.ValueChanged, DistanceBox.ValueChanged
If (GasAmountBox.Value = 0) Then
MileageOutput.Text = "---"
Else
Dim mileage = DistanceBox.Value / GasAmountBox.Value
MileageOutput.Text = mileage.ToString() & " MPG"
End If
End SubContext
StackExchange Code Review Q#90224, answer score: 6
Revisions (0)
No revisions yet.