[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 311
  • Last Modified:

How to graph my program in my VB mortage calculator




Imports System.IO

Public Class MortgageCalculator
    Private LoanTerms(2) As Double         'Array to hold loan terms
    Private InterestRates(2) As Decimal    'Array to hold interest rates


    Private Sub MortgageCalculator_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        'Initialize array for the 3 loan terms
        LoanTerms(0) = 7
        LoanTerms(1) = 15
        LoanTerms(2) = 30

        'Read interest rates from file
        Dim fs As New FileStream("mortgage.txt", FileMode.Open, FileAccess.Read)

        Dim sr As New StreamReader(fs)
        sr.BaseStream.Seek(0, SeekOrigin.Begin)

        'Populate the Interest rates from file
        InterestRates(0) = sr.ReadLine
        InterestRates(1) = sr.ReadLine
        InterestRates(2) = sr.ReadLine
        'Close file
        sr.Close()

        'Initialize text for radio buttons for Mortgate selection
        rdoMortgage1.Text = rdoMortgage1.Text & " at " & InterestRates(0).ToString & " %"
        rdoMortgage2.Text = rdoMortgage2.Text & " at " & InterestRates(1).ToString & " %"
        rdoMortage3.Text = rdoMortage3.Text & " at " & InterestRates(2).ToString & " %"

        'Display an initial loan amount
        txtLoanAmount.Text = FormatCurrency(200000, 2)

    End Sub
    'Calculate all 3 mortgages when the Calculate button is clicked
    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
        Dim loanAmount As Double
        Dim loanPayment As Double
        Dim intRate As Decimal
        Dim loanTerm As Double

        Dim monthlyIntRate As Double
        Dim loanBalance As Double
        Dim principalPayment As Double
        Dim interestPayment As Double
        Dim cumLoanPayment As Double

        'Clear the list box
        ListBox1.Items.Clear()

        If txtLoanAmount.Text = "" Or txtLoanAmount.Text = "0" Or txtInterest1.Text = "" Or txtInterest1.Text = "0" Or txtTerm1.Text = "" Or txtTerm1.Text = "0" Then
            txtMessage.Text = "Please select or enter a mortgage term"
            txtMessage.Visible = True
        Else
            txtMessage.Text = ""
            txtMessage.Visible = False

            'Get the Loan Amount
            loanAmount = txtLoanAmount.Text

            'Get the first Term and Interest
            'Multiply by 12 to convert to months
            loanTerm = txtTerm1.Text * 12
            'Divide displayed interest by 100
            intRate = txtInterest1.Text / 100

            'Get the Loan Amount
            loanAmount = txtLoanAmount.Text

            'Calculate the Payment
            loanPayment = (loanAmount) * (Math.Pow((1 + intRate / 12), loanTerm) * intRate) / (12 * (Math.Pow((1 + intRate / 12), loanTerm) - 1))

            'Get the monthly interest rate for the first loan
            monthlyIntRate = intRate / 12
            loanBalance = loanAmount
            cumLoanPayment = loanAmount

            ListBox1.Items.Add("Period".PadRight(10) & "Balance".PadRight(25) & "Interest Payment".PadRight(25) & "Monthly Payment")

            'Loop throught the number of months for the first term of the loan
            For i As Integer = 1 To loanTerm
                'Calculate monthly interest for period
                interestPayment = loanBalance * monthlyIntRate

                'Calculate the principal amount which is the monthly loan payment substract the interest for the period
                principalPayment = loanPayment - interestPayment

                cumLoanPayment = cumLoanPayment + interestPayment

                'Display the Loan Balance, Interest Payment and Monthly Payment in a Listbox
                ListBox1.Items.Add(i.ToString.PadRight(10) & FormatCurrency(loanBalance, 2).PadRight(25) & FormatCurrency(interestPayment, 2).PadRight(25) & FormatCurrency(loanPayment, 2).PadRight(25))

                'Calculate the loan balance which is the current balance substract the prinicipal amount
                loanBalance = loanBalance - principalPayment
            Next
        End If

    End Sub
    'Close the form when Close button is clicked
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub rdoMortgage1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoMortgage1.CheckedChanged
        'Reset amounts to zero
        txtTerm1.Text = FormatNumber(LoanTerms(0), 0)
        txtInterest1.Text = FormatNumber(InterestRates(0), 2)

        ' Clear the list box of payment schedule
        ListBox1.Items.Clear()

        'Clear and hide the message box
        txtMessage.Text = ""
        txtMessage.Visible = False
    End Sub

    Private Sub rdoMortgage2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoMortgage2.CheckedChanged
        'Reset amounts to zero
        txtTerm1.Text = FormatNumber(LoanTerms(1), 0)
        txtInterest1.Text = FormatNumber(InterestRates(1), 2)

        ' Clear the list box of payment schedule
        ListBox1.Items.Clear()

        'Clear and hide the message box
        txtMessage.Text = ""
        txtMessage.Visible = False
    End Sub

    Private Sub rdoMortage3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoMortage3.CheckedChanged
        'Reset amounts to zero
        txtTerm1.Text = FormatNumber(LoanTerms(2), 0)
        txtInterest1.Text = FormatNumber(InterestRates(2), 2)

        ' Clear the list box of payment schedule
        ListBox1.Items.Clear()

        'Clear and hide the message box
        txtMessage.Text = ""
        txtMessage.Visible = False
    End Sub


    Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click

        'Uncheck all radio buttons for mortgage
        rdoMortgage1.Checked = False
        rdoMortgage2.Checked = False
        rdoMortage3.Checked = False

        'Reset amounts to zero
        txtInterest1.Text = ""
        txtTerm1.Text = ""

        ' Clear the list box of payment schedule
        ListBox1.Items.Clear()

        ' Clear out the message box
        txtMessage.Text = ""
        txtMessage.Visible = False
    End Sub

End Class


0
imagekrazy
Asked:
imagekrazy
  • 2
  • 2
1 Solution
 
Jon500Commented:
You missed a parenthesis.

Seriously, what is your question???

Jon500
0
 
imagekrazyAuthor Commented:
I want to, over the term of the loan.,add graphics in the form of a chart.
0
 
Jon500Commented:
Charts take a LOT of code to write. Are you sure you're up to the task?

Why not use a professional solution? I use this: www.chartfx.com

Let me know what you think.

Regards,
Jon500
0
 
imagekrazyAuthor Commented:
chartfx is way to  expensive,  if you can tell me how to go about it ,that would be cool.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now