Home Programming VB.NET Loan Calculator Example using DataGridView

Search

VB.NET Loan Calculator Example using DataGridView Print
Written by Chris Gountanis   

Visual Studio VB.NET Mortgage Loan Calculator Example Project with Source CodeThis Mortgage (Loan) Calculator application was created in VB.NET using a Progress Bar and a Data Grid View control. I was tired of the multi-line text box formatting issues and decided to step is up a notch. I hope this helps a few beginner VB.NET coders or programming students advance beyond the basic requirements for output. The results of this loan payment calculator are for comparison purposes only. They will be a close approximation of actual loan repayments if available at the terms entered, from a financial institution. This is being provided for you to plan your next loan application. To use, enter values for the Loan Amount, Number of Months for Loan, and the Interest Rate optionand click the Calculate button. Clicking the Reset button will clear entered values.

 

Example Mortgage Loan Calculator with Source Code VB.NET

 

Option Explicit On

'Programmer: Chris Gountanis

'Change Request #19 10/27/2008

'Write the program in VB.Net (not Web based). Use a loan of $200,000 and have

it calculate the payment amount for three mortgage loans:

'- 7 years at 5.35%

'- 15 years at 5.5%

'- 30 years at 5.75%

'Use an array for the different loans. Display the mortgage payment amount

for each loan. Then, list the loan balance and interest paid for each payment

over the term of the loan. Insert comments to document the program.

 

Public Class frmMain

Private Sub btnCalulate_Click(ByVal sender As System.Object, ByVal e As

System.EventArgs) Handles btnCalulate.Click

Try

Dim
LoanAmount As Double

Dim Payment As DoubleDim

InterestRate As Double

Dim Interest As DoubleDim

Principle As Double

Dim Years As IntegerDim

PaymentPeriods As Integer

Dim LoanOptionSelection As Integer

 

'setup loan options array with values

Dim LoanOptions(3, 2) As Double

LoanOptions(0, 0) = 7

LoanOptions(0, 1) = 5.35

LoanOptions(1, 0) = 15

LoanOptions(1, 1) = 5.5

LoanOptions(2, 0) = 30

LoanOptions(2, 1) = 5.75

 

'validate loan amount input

If IsNumeric(txtLoanAmount.Text) = False Then

MsgBox("Loan amount must be numeric. Please enter a validloan amount.")

txtLoanAmount.Clear()

txtLoanAmount.Focus()

Exit Sub

End If

 

'set loan variables

LoanAmount = txtLoanAmount.Text

 

'set proper rate and years depending on option selected by user

Select Case True

Case
optLoanOption1.Checked

LoanOptionSelection = 0

Case optLoanOption2.Checked

LoanOptionSelection = 1

Case optLoanOption3.Checked

LoanOptionSelection = 2

End Select

 

'using array and loan option set the rate and years

InterestRate = LoanOptions(LoanOptionSelection, 1)

Years = LoanOptions(LoanOptionSelection, 0)

 

'calulate total payment periods

PaymentPeriods = Years * 12

 

'if rate is in percent form convert to decimal

If InterestRate > 1 Then InterestRate = InterestRate / 100

 

'calculate monthly payment and return value

Payment = (LoanAmount * Math.Pow((InterestRate / 12) + 1,

(PaymentPeriods)) * InterestRate / 12) / (Math.Pow(InterestRate / 12 + 1,

(PaymentPeriods)) - 1)

 

'display calculated payment

lblMonthlyPayment.Text = "Monthly Payment: " &

FormatCurrency(Payment)

 

'clear datagrid view control (remove all rows)

dgvLoanDetails.Rows.Clear()

 

'setup progress bar

pbLoan.Minimum = 1

pbLoan.Maximum = PaymentPeriods

pbLoan.Value = 1

pbLoan.Visible = True

 

'loop the loan payment periods displaying loan details

Dim i As Integer

For i = 1 To PaymentPeriods

 

'increase progress bar

pbLoan.Value = i

 

'set interest

Interest = (LoanAmount * InterestRate) / 12

 

'set loan amount

LoanAmount = (LoanAmount - Payment) + Interest

 

'set principle

Principle = Payment - Interest

 

'output loan details

dgvLoanDetails.Rows.Add()

dgvLoanDetails.Item(
"Month", i - 1).Value = idgvLoanDetails.Item("Payment", i - 1).Value =

FormatCurrency(Payment)

dgvLoanDetails.Item(
"Interest", i - 1).Value =

FormatCurrency(Interest)

dgvLoanDetails.Item(
"Principle", i - 1).Value =

FormatCurrency(Principle)

dgvLoanDetails.Item(
"LoanAmount", i - 1).Value =

FormatCurrency(LoanAmount)

Next

 

'hide progress bar now that processing is completed

pbLoan.Visible = False Catch ex As Exception

MsgBox("Error: " & ex.Message.ToString())

End Try

End Sub

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As

System.EventArgs) Handles btnClear.Click

 

'clear fields

txtLoanAmount.Clear()

optLoanOption1.Checked = True

lblMonthlyPayment.Text = ""

txtLoanAmount.Text = "200000"

dgvLoanDetails.Rows.Clear()

End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As

System.EventArgs) Handles btnExit.Click

 

'end program

End

End Sub

End Class

 

Downloads:

Source Code Visual Studio 2005

Source Code Visual Studio 2008

 

VB.NET Loan Calculator Example

Source Code Visual Studio 2008 (Option Buttons Replaced with User Input Text Boxes)

Last Updated on Wednesday, 21 July 2010 12:16