贷款计算器使用financial.ppmt方法

时间:2016-05-03 00:11:07

标签: vb.net

任务是让用户输入本金金额以及从组合框中选择利率以及条款。用户界面应显示标签中的每月付款以及该付款的多少用于本金以及多线文本框中的利息。我在完成代码时遇到问题。我不太确定我是不是完全没有获得financial.ppmt方法,或者不仅仅是我的月付款没有准确显示但是我无法获得多行文本框信息(本金额)和利息金额)以正确显示。如果有人可以提供帮助或至少指向我类似的计划,我将非常感谢!

Option Explicit On
Option Strict On
Option Infer Off


Public Class MainForm
Private Sub ExitButton_Click(sender As Object, e As EventArgs) Handles ExitButton.Click
    Me.Close()

End Sub

Private Sub CalcButton_Click(sender As Object, e As EventArgs) Handles CalcButton.Click
    'Calculates the monthly payments on a loan using 
    'annual interest rates from 2%-10% and terms from 1-30 years

    Dim Principal As Double
    Dim term As Integer
    Dim rate As Double
    Dim monthlyPayment As Double
    Dim interest As Double

    'assign input to variables
    Double.TryParse(PrincipalTextBox.Text, Principal)
    term = Convert.ToInt32(TermComboBox.SelectedItem)

    'clear text boxes
    PaymentValue.Text = String.Empty
    PrincipleAndInterestBox.Text = String.Empty

    'calculate and display monthly payments
    monthlyPayment = -Financial.Pmt(rate / 12, 12, term * 12, Principal)
    MonthlyPaymentLabel.Text = monthlyPayment.ToString("C2")

    'Calculate the amount applied to principal and interest
    For per As Integer = 12 To 1 Step -1
        Principal = -Financial.PPmt(rate / 12, per, 12, Principal)
        interest = monthlyPayment - Principal
        PrincipleAndInterestBox.Text = Principal.ToString("C2") & "         " & interest.ToString("C2") & ControlChars.NewLine

    Next per
    PrincipleAndInterestBox.Focus()
End Sub

Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles Me.Load
    'fill termComboBox
    For term As Integer = 1 To 30
        TermComboBox.Items.Add(term.ToString)
    Next term
    TermComboBox.SelectedItem = "10"

    'fill interestRateComboBox
    For rate As Integer = 2 To 10
        InterestRateComboBox.Items.Add(rate.ToString)
    Next rate
    InterestRateComboBox.SelectedItem = "4"
End Sub

结束班

1 个答案:

答案 0 :(得分:0)

转到文本框属性并选择multiline = true

在循环中,您正在为文本框分配新值并错过现有值。

PrincipleAndInterestBox.Text = PrincipleAndInterestBox.Text & Principal.ToString("C2") & "         " & interest.ToString("C2") & ControlChars.NewLine

textBox1.Multiline = True
textBox1.ScrollBars = ScrollBars.Vertical
textBox1.WordWrap = True
textBox1.Text = "Welcome!" & Environment.NewLine & "Second Line"