Userform that calculates gas mileage 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!

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 &lt;= 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 &lt;= 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 &lt;= 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 &lt;= 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 &amp; mileage


    MsgBox("All done!")
End Sub


The sub I use to populate the form follows:

Private Sub PopulateForm(Optional ByVal strGas As String = "", Optional ByVal strDistance As String = "", Optional ByVal strMileage As String = "") 'wipes form or populates with new values

    lblGas.Text = "Gas Consumed: " &amp; strGas
    lblDistance.Text = "Distance Travelled: " &amp; strDistance
    lblMileage.Text = "Mileage: " &amp; strMileage

End Sub
You have a form -- by definition, a window in which text boxes and such can appear. Why are you using 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() &amp; " MPG"
    End If
End Sub


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.