mardi 5 mai 2015

A first chance of exception type 'System.FormatException' occurred in mscorlib.dll

I am having some major issues with a program I am writing. I have gotten this far but I can't find the issue with my code at all. The program is supposed to calculate the total cost of an appliance. All of the appliances in the list work except for the "Washer" which requires me to take the "CostPerGallon" and the "GallonsUsed" and multiply them to find a WaterCost. I add the WaterCostand the "CostPerDay" I've calculated to find the total. For some reason I'm getting this exception ONLY when I try to calculate the Washer cost. I have put "TryParse" into both the "GallonsUsed" and "CostPerGallon" inputs to try and catch and issues, but that's still not working. Here is the code:

Public Class AuditWindow 'Declare all of the variables that will be used in the main method Dim dblCostPerDay As Double Dim dblCostPerHour As Double Dim dblHoursPerDay As Double Dim dblPowerNeeded As Double Dim dblGallonsDay As Double Dim dblCostPerGallon As Double Dim txtApplianceSelection As String Dim txtListString As String Dim blnCondition = False Dim dblTotalCost As Double Dim dblWaterCost As Double 'Method to validate the text entered into Cost per kilowatt-hour. Private Sub txtCostPerHour_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCostPerHour.TextChanged Dim Character As String Dim NNN As Integer Dim NumOfPeriods As Integer = 0 Dim NumOfMinus As Integer = 0 Dim OtherChar As Boolean = False For NNN = 1 To Len(txtCostPerHour.Text) Character = Mid(txtCostPerHour.Text, NNN, 1) If Character = "." Then NumOfPeriods = NumOfPeriods + 1 If (Character < "0" OrElse Character > "9") AndAlso Character <> "." Then OtherChar = True Next NNN If NumOfPeriods < 2 And OtherChar = False Then 'Valid number entered lblErrorMsg.Visible = False lblErrorMsgTwo.Visible = False Else 'Not a Valid number lblErrorMsg.Text = "Cost per kilowatt-hour is not a valid number." lblErrorMsgTwo.Text = "Please click Clear Inputs to try another input." lblErrorMsg.Visible = True lblErrorMsgTwo.Visible = True End If End Sub 'Method to validate the text entered into Power Needed. Private Sub txtPowerNeeded_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPowerNeeded.TextChanged Dim Character As String Dim NNN As Integer Dim NumOfPeriods As Integer = 0 Dim NumOfMinus As Integer = 0 Dim OtherChar As Boolean = False For NNN = 1 To Len(txtPowerNeeded.Text) Character = Mid(txtPowerNeeded.Text, NNN, 1) If Character = "." Then NumOfPeriods = NumOfPeriods + 1 If (Character < "0" OrElse Character > "9") AndAlso Character <> "." Then OtherChar = True Next NNN If NumOfPeriods < 2 And OtherChar = False Then 'Valid number entered lblErrorMsg.Visible = False lblErrorMsgTwo.Visible = False Else 'Not a Valid number lblErrorMsg.Text = "Power needed is not a valid number." lblErrorMsgTwo.Text = "Please click Clear Inputs to try another input." lblErrorMsg.Visible = True lblErrorMsgTwo.Visible = True End If End Sub 'Method to validate the text entered into Hours per day. Private Sub txtHoursDay_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtHoursDay.TextChanged Dim Character As String Dim NNN As Integer Dim NumOfPeriods As Integer = 0 Dim NumOfMinus As Integer = 0 Dim OtherChar As Boolean = False For NNN = 1 To Len(txtHoursDay.Text) Character = Mid(txtHoursDay.Text, NNN, 1) If Character = "." Then NumOfPeriods = NumOfPeriods + 1 If (Character < "0" OrElse Character > "9") AndAlso Character <> "." Then OtherChar = True Next NNN If NumOfPeriods < 2 And OtherChar = False Then 'Valid number entered lblErrorMsg.Visible = False lblErrorMsgTwo.Visible = False Else 'Not a Valid number lblErrorMsg.Text = "Hours Per Day is not a valid number." lblErrorMsgTwo.Text = "Please click Clear Inputs to try another input." lblErrorMsg.Visible = True lblErrorMsgTwo.Visible = True End If End Sub

Private Sub txtCostPerGallon_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCostPerGallon.TextChanged
    Dim Character As String
    Dim NNN As Integer
    Dim NumOfPeriods As Integer = 0
    Dim NumOfMinus As Integer = 0
    Dim OtherChar As Boolean = False
    For NNN = 1 To Len(txtCostPerGallon.Text)
        Character = Mid(txtCostPerGallon.Text, NNN, 1)
        If Character = "." Then NumOfPeriods = NumOfPeriods + 1
        If (Character < "0" OrElse Character > "9") AndAlso Character <> "." Then OtherChar = True
    Next NNN
    If NumOfPeriods < 2 And OtherChar = False Then
        'Valid number entered
        lblErrorMsg.Visible = False
        lblErrorMsgTwo.Visible = False
    Else
        'Not a Valid number
        lblErrorMsg.Text = "Cost Per Gallon is not a valid number."
        lblErrorMsgTwo.Text = "Please click Clear Inputs to try another input."
        lblErrorMsg.Visible = True
        lblErrorMsgTwo.Visible = True
    End If
End Sub

Private Sub txtGallonsUsed_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtGallonsUsed.TextChanged
    Dim Character As String
    Dim NNN As Integer
    Dim NumOfPeriods As Integer = 0
    Dim NumOfMinus As Integer = 0
    Dim OtherChar As Boolean = False
    For NNN = 1 To Len(txtGallonsUsed.Text)
        Character = Mid(txtGallonsUsed.Text, NNN, 1)
        If Character = "." Then NumOfPeriods = NumOfPeriods + 1
        If (Character < "0" OrElse Character > "9") AndAlso Character <> "." Then OtherChar = True
    Next NNN
    If NumOfPeriods < 2 And OtherChar = False Then
        'Valid number entered
        lblErrorMsg.Visible = False
        lblErrorMsgTwo.Visible = False
    Else
        'Not a Valid number
        lblErrorMsg.Text = "Gallons Used is not a valid number."
        lblErrorMsgTwo.Text = "Please click Clear Inputs to try another input."
        lblErrorMsg.Visible = True
        lblErrorMsgTwo.Visible = True
    End If
End Sub
'This is the main logic of the application. When the user clicks "Submit" the application will run through all of the validations first and determine if any error messages should be shown
Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
    If txtCostPerHour.Text = Nothing Then
        lblErrorMsg.Text = "You must enter a value into Cost Per Hour."
        lblErrorMsg.Visible = True
    ElseIf Not lstAppliances.SelectedIndex >= 0 Then
        lblErrorMsg.Text = "You must select an appliance."
        lblErrorMsg.Visible = True
    ElseIf txtHoursDay.Text = Nothing Then
        lblErrorMsg.Text = "You must enter a value into Hours Per Day."
        lblErrorMsg.Visible = True
    ElseIf txtPowerNeeded.Text = Nothing Then
        lblErrorMsg.Text = "You must enter Power Needed."
        lblErrorMsg.Visible = True
    ElseIf lstAppliances.SelectedItem = "Washer" And txtCostPerGallon.Text = Nothing Then
        lblErrorMsg.Text = "If Washer is selected, you must enter a Cost Per Gallon."
        lblErrorMsg.Visible = True
    ElseIf lstAppliances.SelectedItem = "Washer" And txtGallonsUsed.Text = Nothing Then
        lblErrorMsg.Text = "If Washer is selected, you must enter Gallons Used."
        lblErrorMsg.Visible = True
    ElseIf Convert.ToDouble(txtCostPerHour.Text) < 0 Then
        lblErrorMsg.Text = "Cost Per Hour must be greater than zero."
        lblErrorMsg.Visible = True
    ElseIf Convert.ToDouble(txtCostPerHour.Text) > 1 Then
        lblErrorMsg.Text = "Cost Per Hour must be less than one."
        lblErrorMsg.Visible = True
    ElseIf Convert.ToDouble(txtPowerNeeded.Text) < 0 Then
        lblErrorMsg.Text = "Power Needed must be greater than zero."
        lblErrorMsg.Visible = True
    ElseIf Convert.ToDouble(txtPowerNeeded.Text) > 2 Then
        lblErrorMsg.Text = "Power Needed must be less than two."
        lblErrorMsg.Visible = True
    ElseIf Convert.ToDouble(txtHoursDay.Text) < 0 Then
        lblErrorMsg.Text = "Hours Per Day must be greater than zero."
        lblErrorMsg.Visible = True
    ElseIf Convert.ToDouble(txtHoursDay.Text) > 24 Then
        lblErrorMsg.Text = "Hours Per Day must be less than 24."
        lblErrorMsg.Visible = True
    ElseIf lstAppliances.SelectedItem = "Washer" Then
        If Double.TryParse(txtCostPerGallon.Text, dblCostPerGallon) = False Then
            lblErrorMsg.Text = "Cost Per Gallon must be a number."
            lblErrorMsg.Visible = True
        Else
            dblCostPerGallon = Convert.ToDouble(txtCostPerGallon.Text)
        End If
    ElseIf lstAppliances.SelectedItem = "Washer" Then
        If Double.TryParse(txtGallonsUsed.Text, dblGallonsDay) = False Then
            lblErrorMsg.Text = "Gallons Used must be a number."
            lblErrorMsg.Visible = True
        Else
            dblGallonsDay = Convert.ToDouble(txtGallonsUsed.Text)
        End If
    Else 'If all of the inputs pass validations, the calculation will take place
        dblCostPerHour = Convert.ToDouble(txtCostPerHour.Text)
        dblHoursPerDay = Convert.ToDouble(txtHoursDay.Text)
        dblPowerNeeded = Convert.ToDouble(txtPowerNeeded.Text)
        If lstAppliances.SelectedItem = "Washer" Then
            dblCostPerDay = dblCostPerHour * dblHoursPerDay * dblPowerNeeded
            dblWaterCost = dblCostPerGallon * dblGallonsDay
            dblCostPerDay = dblCostPerDay + dblWaterCost
        Else
            dblCostPerDay = dblCostPerHour * dblHoursPerDay * dblPowerNeeded
        End If
        dblTotalCost = dblTotalCost + dblCostPerDay
        txtApplianceSelection = lstAppliances.SelectedItem.ToString
        txtListString = (txtApplianceSelection & "     " & dblHoursPerDay & "     $" & Math.Round(dblCostPerDay, 2) & "     $" & Math.Round(dblTotalCost, 2)) 'Here we build our string
        ListView1.Items.Add(txtListString)

    End If
End Sub
'This clears out all of the inputs for the user
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
    txtCostPerHour.Clear()
    txtHoursDay.Clear()
    txtPowerNeeded.Clear()
    lstAppliances.ClearSelected()
    lstAppliances.SetSelected(0, True)
End Sub
'This exits the program
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
    Me.Close()
End Sub

End Class

Aucun commentaire:

Enregistrer un commentaire