In visual studio I am suppose to create a distance to metric converter that has
ID: 3798782 • Letter: I
Question
In visual studio I am suppose to create a distance to metric converter that has three group boxes one with 2 RButoons the other two with three RButtons each. Also a textbox, label and button. I have gotten everything working properly however I do not know how to get all the numbers to round to three decimal places and I also need to know what to put in the code to prevent someone from crashing it. Such as maybe at the end it should only be able to enter numbers and not letters. Entering letters will crash it. Please help, here is code:
Public Class Form1
Private Sub btnConvert_Click(sender As Object, e As EventArgs) Handles btnConvert.Click
Dim x As Double
Dim y As Double
'if text input is not empty
If (txtInput.Text <> "") Then
'convert text to integer using Convert.ToInt32 method
'now you have user given input value
x=Convert.ToInt32(txtInput.Text)
else
'display a message box if text input is empty
MessageBox.Show("Input the digit to convert")
End If
y = 0
'convert Feet to Millimeters
If (optFeet.Checked = True And optMillimeters.Checked = True) Then
y = x * 0.305
End If
'rest of the code is same no changes
'convert Feet to Meters
If (optFeet.Checked = True And optMeters.Checked = True) Then
y = x * 0.3048
End If
'convert Feet to Centimeters
If (optFeet.Checked = True And optCentimeters.Checked = True) Then
y = x * 30.48
End If
'convert Inches to Millimeters
If (optInches.Checked = True And optMillimeters.Checked = True) Then
y = x * 25.4
End If
'convert Inches to Meters
If (optInches.Checked = True And optMeters.Checked = True) Then
y = x * 0.025
End If
'convert Inches to Centimeters
If (optInches.Checked = True And optCentimeters.Checked = True) Then
y = x * 2.54
End If
'convert Yards to Millimeters
If (optYards.Checked = True And optMillimeters.Checked = True) Then
y = x * 914.4
End If
'convert Yards to Meters
If (optYards.Checked = True And optMeters.Checked = True) Then
y = x * 0.914
End If
'convert Yards to Centimeters
If (optYards.Checked = True And optCentimeters.Checked = True) Then
y = x * 91.44
End If
'Now set value of y to corresponding text box
'we assume that name of the text box is outputTextBox
outputTextBox.Text=Convert.ToString(y)
End Sub
End Class
Explanation / Answer
Hi,
this is simple.
find the corrected code below:
Public Class Form1
Private Sub btnConvert_Click(sender As Object, e As EventArgs) Handles btnConvert.Click
Dim x As Double
Dim y As Double
'if text input is not empty
If (txtInput.Text <> "") Then
'convert text to integer using Convert.ToInt32 method
'now you have user given input value
Try
x=Convert.ToInt32(txtInput.Text)
Catch ex As Exception
MessageBox.Show("please check the input, only numeric digits are allowed")
End Try
else
'display a message box if text input is empty
MessageBox.Show("Input the digit to convert")
End If
y = 0.000
'convert Feet to Millimeters
If (optFeet.Checked = True And optMillimeters.Checked = True) Then
y = Math.Round(x * 0.305,3)
End If
'rest of the code is same no changes
'convert Feet to Meters
If (optFeet.Checked = True And optMeters.Checked = True) Then
y = Math.Round(x * 0.3048,3)
End If
'convert Feet to Centimeters
If (optFeet.Checked = True And optCentimeters.Checked = True) Then
y = Math.Round(x * 30.48,3)
End If
'convert Inches to Millimeters
If (optInches.Checked = True And optMillimeters.Checked = True) Then
y = Math.Round(x * 25.4,3)
End If
'convert Inches to Meters
If (optInches.Checked = True And optMeters.Checked = True) Then
y = Math.Round(x * 0.025,3)
End If
'convert Inches to Centimeters
If (optInches.Checked = True And optCentimeters.Checked = True) Then
y = Math.Round(x * 2.54,3)
End If
'convert Yards to Millimeters
If (optYards.Checked = True And optMillimeters.Checked = True) Then
y = Math.Round(x * 914.4,3)
End If
'convert Yards to Meters
If (optYards.Checked = True And optMeters.Checked = True) Then
y = Math.Round(x * 0.914,3)
End If
'convert Yards to Centimeters
If (optYards.Checked = True And optCentimeters.Checked = True) Then
y = Math.Round(x * 91.44,3)
End If
'Now set value of y to corresponding text box
'we assume that name of the text box is outputTextBox
outputTextBox.Text=Convert.ToString(y)
End Sub
End Class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.