Hello everyone!!

I have an assignment to create that wonderful mortgage calculator. I have the code working, but when i try to catch an error (No matter is it is left blank, or a letter is typed in) the program crashes. Not sure what i am doing wrong, but any in site would be great. Thanks everyone!!

Public Class MortCalc

    Private Sub clearForm()
        txtLoan.Text = 200000
        txtInterest.Text = 5.75
        txtYears.Text = 30
        txtTotal.Text = ""
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
        'New Varables
        Dim txtPrincipal As Double
        Dim txtRate As Double
        Dim txtYears As Integer
        Dim amount As Double
        Dim monthlyRate As Double
        Dim years As Integer

        amount = CDbl(Me.txtLoan.Text)
        monthlyRate = CDbl(Me.txtInterest.Text) / 100 'allows interest rate to be entered whole number'
        years = CInt(Me.txtYears.Text)

        If IsNumeric(txtLoan.Text) = False Then
            MsgBox("Please enter a valid loan amount.")
            txtLoan.Clear()
            txtLoan.Focus()
            Exit Sub
        End If

        If IsNumeric(txtInterest.Text) = False Then
            MsgBox("Please enter a valid Interest Rate")
            txtInterest.Clear()
            txtInterest.Focus()
            Exit Sub
        End If

        If IsNumeric(Me.txtYears.Text) = False Then
            MsgBox("Please enter a valid year")
            Me.txtYears.Clear()
            Me.txtYears.Focus()
        End If



        txtPrincipal = FormatCurrency(amount)
        txtRate = FormatCurrency(monthlyRate)
        txtYears = FormatCurrency(years) * 12

        Dim iRate As Double = monthlyRate / 12
        Dim tTotal As Double = txtPrincipal * (iRate / (1 - (1 + iRate) ^ (-txtYears)))
        Me.txtTotal.Text = Format(tTotal, "$#,##0.00")
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        clearForm()
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        End
    End Sub
End Class

This is because even though you are validating the textboxes' input, you are not stopping the program's progress... what you what to do is separate the FormatCurrency() functions into a separate function, validated by the IsNumer() results. Something like this:

Public Class MortCalc

    Private Sub clearForm()
        txtLoan.Text = 200000
        txtInterest.Text = 5.75
        txtYears.Text = 30
        txtTotal.Text = ""
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
        'New Varables
        Dim txtPrincipal As Double
        Dim txtRate As Double
        Dim txtYears As Integer
        Dim amount As Double
        Dim monthlyRate As Double
        Dim years As Integer

        amount = CDbl(Me.txtLoan.Text)
        monthlyRate = CDbl(Me.txtInterest.Text) / 100 'allows interest rate to be entered whole number'
        years = CInt(Me.txtYears.Text)

        If Not IsNumeric(txtLoan.Text) Then
            MsgBox("Please enter a valid loan amount.")
            txtLoan.Clear()
            txtLoan.Focus()
            Exit Sub
        End If

        If Not IsNumeric(txtInterest.Text) Then
            MsgBox("Please enter a valid Interest Rate")
            txtInterest.Clear()
            txtInterest.Focus()
            Exit Sub
        End If

        If Not IsNumeric(Me.txtYears.Text) Then
            MsgBox("Please enter a valid year")
            Me.txtYears.Clear()
            Me.txtYears.Focus()
        End If


         If IsNumeric(txtLoan.Text) And IsNumeric(txtInterest.Text) And IsNumeric(txtYears.Text) Then
            txtPrincipal = FormatCurrency(amount)
            txtRate = FormatCurrency(monthlyRate)
            txtYears = FormatCurrency(years) * 12
            Dim iRate As Double = monthlyRate / 12
            Dim tTotal As Double = txtPrincipal * (iRate / (1 - (1 + iRate) ^ (-txtYears)))
            Me.txtTotal.Text = Format(tTotal, "$#,##0.00")
         End If
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        clearForm()
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        End
    End Sub
End Class

Edited 7 Years Ago by Nichito: n/a

This article has been dead for over six months. Start a new discussion instead.