In: Computer Science
Code, pls. Thank you.
Exercise - MPG Calculator Create a Windows Forms application and call it MPGCalculator Build a form that looks like the form in the video provided. The form consists of 4 labels, 2 textboxes and 3 buttons Name all controls properly. Certain labels do not have to be named Pay attention to video and set all design time properties accordingly. The "x" should be a hot key for the exit button When Calculate is clicked, you should calculate the MPG and display it. Round to 2 decimal places. (google it!) When Clear is clicked, clear the form. When Exit is clicked, close the form
Below is the solution:
Public Class frmMilesPerGallon
Private Sub frmMilesPerGallon_Load(sender As
Object, e As EventArgs) Handles MyBase.Load
txtGallon.Text =
""
txtMiles.Text = ""
Label3.Text = "MPG is:
"
label4.Text = "0"
End Sub
'Exit Events
Private Sub btnExit_Click(sender As Object, e As
EventArgs) Handles btnExit.Click
Me.Close()
End Sub
'calculate button event
Private Sub btnCalculate_Click(sender As Object,
e As EventArgs) Handles btnCalculate.Click
Dim miles As
Integer
Dim gallon As
Integer
Dim mpg As Double
If
(Integer.TryParse(txtMiles.Text, miles) And
Integer.TryParse(txtGallon.Text, gallon)) Then 'Accept the miles
and gallon integer value
mpg = miles / gallon 'miles dived by gallon will get the
result
label4.Text = mpg.ToString("0.00")
Else
MessageBox.Show("Enter valid value!")
End If
End Sub
'clear button event will clear the all form
object value
Private Sub btnClear_Click(sender As Object, e
As EventArgs) Handles btnClear.Click
txtGallon.Clear()
txtMiles.Clear()
Label3.Text = "MPG is:
"
label4.Text = "0"
End Sub
End Class