?
Solved

How do I add a graph in my Visual Basic program

Posted on 2009-12-28
3
Medium Priority
?
222 Views
Last Modified: 2013-11-26
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
Comment
Question by:imagekrazy
  • 2
3 Comments
 
LVL 12

Expert Comment

by:rgn2121
ID: 26135579
Use the built in MS control....
http://www.dreamincode.net/forums/showtopic40143.htm
Or try a third party control or something like Zed Graph
http://zedgraph.sourceforge.net/samples.html 
0
 

Author Comment

by:imagekrazy
ID: 26154404
thanks for the info, but what  i need to know is how to graph my program
0
 
LVL 12

Accepted Solution

by:
rgn2121 earned 2000 total points
ID: 26155683
Then you might want to delete this question and start a new one with the Title of How to graph your program instead of how to add a graph, not actually graphing out all of the points for you.  I assumed you just needed to know about adding the graph, which the links provide information to do...
http://www.codeproject.com/KB/graphics/zedgraph.aspx
The link above is a great tutorial on using Zed Graph and there is a ton of info out there on the link I posted earlier.  
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month13 days, 20 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question