D
dc15
For an intro to VB project I have to write a program which takes an
amount of Miles, Yards, and Inches.....and converts it to metric (KM,
M, and CM) when all values are entered to the input text boxes it works
fine, but if there is a number missing I get the following error:
An unhandled exception of type 'System.InvalidCastException' occurred
in microsoft.visualbasic.dll
Additional information: Cast from string "" to type 'Double' is not
valid.
i have already tried a
If txtmiles.Text = "" Then
txmiles.Text = "0"
End If and still didnt work
here is my code if anyone can help thatd be great
Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnCompute.Click
'This string takes the inputted numbers and converts them all
to inches.
Dim inches As String
inches = (txtMiles.Text) * 63360 + (txtYards.Text) * 36 +
(txtFeet.Text) * 12 + (txtInches.Text)
'This part converts CStr(inches) which was calculated above
into Meters
Dim meters As String
meters = CStr(inches) / 39.37
'This calculates the calculates the kilometers, meters, and
centimeters.
Dim Kilometers As String
Kilometers = CStr(meters) / 1000
Int(CStr(Kilometers))
Dim OutputMeters As String
OutputMeters = ((CStr(Kilometers) - Int(CStr(Kilometers))) * 10
^ 3)
Int(CStr(OutputMeters))
Dim Centimeters As String
Centimeters = ((CStr(OutputMeters) - Int(CStr(OutputMeters))) *
10 ^ 2)
'This posts the results to the results boxes
txtKilometers.Text = Int(CStr(Kilometers))
txtmeters.Text = Int(CStr(OutputMeters))
'This will round the centimeters to the decimal place and post
it
Dim n As Double = CStr(Centimeters)
txtCentimeters.Text = (Math.Round(n, 1))
'This will disable the txt box and enable the results
txtMiles.ReadOnly = True
txtYards.ReadOnly = True
txtFeet.ReadOnly = True
txtInches.ReadOnly = True
txtmeters.ReadOnly = False
txtCentimeters.ReadOnly = False
txtKilometers.ReadOnly = False
End Sub
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnReset.Click
'This button will reset and clear all the text boxes
txtMiles.Clear()
txtYards.Clear()
txtFeet.Clear()
txtInches.Clear()
txtKilometers.Clear()
txtmeters.Clear()
txtCentimeters.Clear()
'This will enable the txt box and disable the results
txtMiles.ReadOnly = False
txtYards.ReadOnly = False
txtFeet.ReadOnly = False
txtInches.ReadOnly = False
txtKilometers.ReadOnly = True
txtmeters.ReadOnly = True
txtCentimeters.ReadOnly = True
End Sub
End Class
amount of Miles, Yards, and Inches.....and converts it to metric (KM,
M, and CM) when all values are entered to the input text boxes it works
fine, but if there is a number missing I get the following error:
An unhandled exception of type 'System.InvalidCastException' occurred
in microsoft.visualbasic.dll
Additional information: Cast from string "" to type 'Double' is not
valid.
i have already tried a
If txtmiles.Text = "" Then
txmiles.Text = "0"
End If and still didnt work
here is my code if anyone can help thatd be great
Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnCompute.Click
'This string takes the inputted numbers and converts them all
to inches.
Dim inches As String
inches = (txtMiles.Text) * 63360 + (txtYards.Text) * 36 +
(txtFeet.Text) * 12 + (txtInches.Text)
'This part converts CStr(inches) which was calculated above
into Meters
Dim meters As String
meters = CStr(inches) / 39.37
'This calculates the calculates the kilometers, meters, and
centimeters.
Dim Kilometers As String
Kilometers = CStr(meters) / 1000
Int(CStr(Kilometers))
Dim OutputMeters As String
OutputMeters = ((CStr(Kilometers) - Int(CStr(Kilometers))) * 10
^ 3)
Int(CStr(OutputMeters))
Dim Centimeters As String
Centimeters = ((CStr(OutputMeters) - Int(CStr(OutputMeters))) *
10 ^ 2)
'This posts the results to the results boxes
txtKilometers.Text = Int(CStr(Kilometers))
txtmeters.Text = Int(CStr(OutputMeters))
'This will round the centimeters to the decimal place and post
it
Dim n As Double = CStr(Centimeters)
txtCentimeters.Text = (Math.Round(n, 1))
'This will disable the txt box and enable the results
txtMiles.ReadOnly = True
txtYards.ReadOnly = True
txtFeet.ReadOnly = True
txtInches.ReadOnly = True
txtmeters.ReadOnly = False
txtCentimeters.ReadOnly = False
txtKilometers.ReadOnly = False
End Sub
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnReset.Click
'This button will reset and clear all the text boxes
txtMiles.Clear()
txtYards.Clear()
txtFeet.Clear()
txtInches.Clear()
txtKilometers.Clear()
txtmeters.Clear()
txtCentimeters.Clear()
'This will enable the txt box and disable the results
txtMiles.ReadOnly = False
txtYards.ReadOnly = False
txtFeet.ReadOnly = False
txtInches.ReadOnly = False
txtKilometers.ReadOnly = True
txtmeters.ReadOnly = True
txtCentimeters.ReadOnly = True
End Sub
End Class