gmahler5th
asked on
Student needs help with sample app in VB.net
I need a sample app in VB that
Using a graphical user interface that calculates and displays the mortgage payment amount from user input of the amount of the mortgage, the term of the mortgage, and the interest rate of the mortgage. Allow the user to loop back and enter new data or quit.
Using a graphical user interface that calculates and displays the mortgage payment amount from user input of the amount of the mortgage, the term of the mortgage, and the interest rate of the mortgage. Allow the user to loop back and enter new data or quit.
ALso have a look at this, also in C#:
http://www.c-sharpcorner.com/Code/2002/Mar/MortgageCalculator.asp
http://www.c-sharpcorner.com/Code/2002/Mar/MortgageCalculator.asp
Hi,
plz post ur email-id and i'll mail something to help u.
plz post ur email-id and i'll mail something to help u.
Here is a sample app that I made for Amortization......
'FORM 1
Imports System.IO
Imports System
Imports System.Drawing.Printing
Imports Microsoft.VisualBasic
Public Class frmAmort
Inherits System.Windows.Forms.Form
Private WithEvents pdoc As New PrintDocument
Private cCollection As New Collection
Private Msg As String
Private sLineTemp As String
Private sFileName As String
Private sLine As String = Space(70)
Private sHeader As String = Space(90)
Private sHeader1 As String = Space(90)
Private printFont As Font
Private streamToPrint As StreamReader
Private storedPageSettings As PageSettings
Private TotPmts As Double
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
InitializeListview()
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.ICon tainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents lblCost As System.Windows.Forms.Label
Friend WithEvents txtCost As System.Windows.Forms.TextB ox
Friend WithEvents btnOK As System.Windows.Forms.Butto n
Friend WithEvents txtPercentage As System.Windows.Forms.TextB ox
Friend WithEvents txtMonths As System.Windows.Forms.TextB ox
Friend WithEvents lvwOut As System.Windows.Forms.ListV iew
Friend WithEvents btnClear As System.Windows.Forms.Butto n
Friend WithEvents lblPercentage As System.Windows.Forms.Label
Friend WithEvents lblMonths As System.Windows.Forms.Label
Friend WithEvents btnSave As System.Windows.Forms.Butto n
Friend WithEvents btnCancel As System.Windows.Forms.Butto n
Friend WithEvents lblIns As System.Windows.Forms.Label
Friend WithEvents txtIns As System.Windows.Forms.TextB ox
Friend WithEvents txtTAX As System.Windows.Forms.TextB ox
Friend WithEvents btnPrint As System.Windows.Forms.Butto n
Friend WithEvents lblTax As System.Windows.Forms.Label
Friend WithEvents ToolTip1 As System.Windows.Forms.ToolT ip
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Cont ainer
Dim resources As System.Resources.ResourceM anager = New System.Resources.ResourceM anager(Get Type(frmAm ort))
Me.btnOK = New System.Windows.Forms.Butto n
Me.lblCost = New System.Windows.Forms.Label
Me.lblPercentage = New System.Windows.Forms.Label
Me.lblMonths = New System.Windows.Forms.Label
Me.txtCost = New System.Windows.Forms.TextB ox
Me.txtPercentage = New System.Windows.Forms.TextB ox
Me.txtMonths = New System.Windows.Forms.TextB ox
Me.lvwOut = New System.Windows.Forms.ListV iew
Me.btnClear = New System.Windows.Forms.Butto n
Me.btnSave = New System.Windows.Forms.Butto n
Me.btnCancel = New System.Windows.Forms.Butto n
Me.lblIns = New System.Windows.Forms.Label
Me.lblTax = New System.Windows.Forms.Label
Me.txtIns = New System.Windows.Forms.TextB ox
Me.txtTAX = New System.Windows.Forms.TextB ox
Me.btnPrint = New System.Windows.Forms.Butto n
Me.ToolTip1 = New System.Windows.Forms.ToolT ip(Me.comp onents)
Me.SuspendLayout()
'
'btnOK
'
Me.btnOK.Font = New System.Drawing.Font("Micro soft Sans Serif", 8.25!, System.Drawing.FontStyle.B old, System.Drawing.GraphicsUni t.Point, CType(0, Byte))
Me.btnOK.Location = New System.Drawing.Point(8, 376)
Me.btnOK.Name = "btnOK"
Me.btnOK.Size = New System.Drawing.Size(60, 24)
Me.btnOK.TabIndex = 3
Me.btnOK.Text = "&Ok"
Me.ToolTip1.SetToolTip(Me. btnOK, "Runs the figures that are posted")
'
'lblCost
'
Me.lblCost.Font = New System.Drawing.Font("Micro soft Sans Serif", 8.25!, System.Drawing.FontStyle.B old, System.Drawing.GraphicsUni t.Point, CType(0, Byte))
Me.lblCost.Location = New System.Drawing.Point(28, 8)
Me.lblCost.Name = "lblCost"
Me.lblCost.Size = New System.Drawing.Size(200, 20)
Me.lblCost.TabIndex = 4
Me.lblCost.Text = "How much do you want to borrow? :"
'
'lblPercentage
'
Me.lblPercentage.Font = New System.Drawing.Font("Micro soft Sans Serif", 8.25!, System.Drawing.FontStyle.B old, System.Drawing.GraphicsUni t.Point, CType(0, Byte))
Me.lblPercentage.Location = New System.Drawing.Point(28, 36)
Me.lblPercentage.Name = "lblPercentage"
Me.lblPercentage.Size = New System.Drawing.Size(196, 28)
Me.lblPercentage.TabIndex = 5
Me.lblPercentage.Text = "What is the annual percentage rate of your loan? :"
'
'lblMonths
'
Me.lblMonths.Font = New System.Drawing.Font("Micro soft Sans Serif", 8.25!, System.Drawing.FontStyle.B old, System.Drawing.GraphicsUni t.Point, CType(0, Byte))
Me.lblMonths.Location = New System.Drawing.Point(28, 72)
Me.lblMonths.Name = "lblMonths"
Me.lblMonths.Size = New System.Drawing.Size(192, 28)
Me.lblMonths.TabIndex = 6
Me.lblMonths.Text = "How many monthly payments do you have to make? :"
'
'txtCost
'
Me.txtCost.Font = New System.Drawing.Font("Micro soft Sans Serif", 8.25!, System.Drawing.FontStyle.B old, System.Drawing.GraphicsUni t.Point, CType(0, Byte))
Me.txtCost.Location = New System.Drawing.Point(232, 8)
Me.txtCost.Name = "txtCost"
Me.txtCost.Size = New System.Drawing.Size(84, 20)
Me.txtCost.TabIndex = 0
Me.txtCost.Text = "txtCost"
'
'txtPercentage
'
Me.txtPercentage.Font = New System.Drawing.Font("Micro soft Sans Serif", 8.25!, System.Drawing.FontStyle.B old, System.Drawing.GraphicsUni t.Point, CType(0, Byte))
Me.txtPercentage.Location = New System.Drawing.Point(232, 40)
Me.txtPercentage.Name = "txtPercentage"
Me.txtPercentage.Size = New System.Drawing.Size(84, 20)
Me.txtPercentage.TabIndex = 1
Me.txtPercentage.Text = "txtPercentage"
'
'txtMonths
'
Me.txtMonths.Font = New System.Drawing.Font("Micro soft Sans Serif", 8.25!, System.Drawing.FontStyle.B old, System.Drawing.GraphicsUni t.Point, CType(0, Byte))
Me.txtMonths.Location = New System.Drawing.Point(232, 76)
Me.txtMonths.Name = "txtMonths"
Me.txtMonths.Size = New System.Drawing.Size(84, 20)
Me.txtMonths.TabIndex = 2
Me.txtMonths.Text = "txtMonths"
'
'lvwOut
'
Me.lvwOut.Font = New System.Drawing.Font("Micro soft Sans Serif", 8.25!, System.Drawing.FontStyle.B old, System.Drawing.GraphicsUni t.Point, CType(0, Byte))
Me.lvwOut.Location = New System.Drawing.Point(12, 164)
Me.lvwOut.Name = "lvwOut"
Me.lvwOut.Size = New System.Drawing.Size(316, 200)
Me.lvwOut.TabIndex = 12
'
'btnClear
'
Me.btnClear.Font = New System.Drawing.Font("Micro soft Sans Serif", 8.25!, System.Drawing.FontStyle.B old, System.Drawing.GraphicsUni t.Point, CType(0, Byte))
Me.btnClear.Location = New System.Drawing.Point(136, 376)
Me.btnClear.Name = "btnClear"
Me.btnClear.Size = New System.Drawing.Size(64, 24)
Me.btnClear.TabIndex = 15
Me.btnClear.Text = "&Clear"
Me.ToolTip1.SetToolTip(Me. btnClear, "Clears view list")
'
'btnSave
'
Me.btnSave.Font = New System.Drawing.Font("Micro soft Sans Serif", 8.25!, System.Drawing.FontStyle.B old, System.Drawing.GraphicsUni t.Point, CType(0, Byte))
Me.btnSave.Location = New System.Drawing.Point(200, 376)
Me.btnSave.Name = "btnSave"
Me.btnSave.Size = New System.Drawing.Size(64, 24)
Me.btnSave.TabIndex = 17
Me.btnSave.Text = "&Save"
Me.ToolTip1.SetToolTip(Me. btnSave, "You must save before it can be printed")
'
'btnCancel
'
Me.btnCancel.Font = New System.Drawing.Font("Micro soft Sans Serif", 8.25!, System.Drawing.FontStyle.B old, System.Drawing.GraphicsUni t.Point, CType(0, Byte))
Me.btnCancel.Location = New System.Drawing.Point(68, 376)
Me.btnCancel.Name = "btnCancel"
Me.btnCancel.Size = New System.Drawing.Size(68, 24)
Me.btnCancel.TabIndex = 18
Me.btnCancel.Text = "C&ancel"
Me.ToolTip1.SetToolTip(Me. btnCancel, "Closes program")
'
'lblIns
'
Me.lblIns.Font = New System.Drawing.Font("Micro soft Sans Serif", 8.25!, System.Drawing.FontStyle.B old, System.Drawing.GraphicsUni t.Point, CType(0, Byte))
Me.lblIns.Location = New System.Drawing.Point(28, 108)
Me.lblIns.Name = "lblIns"
Me.lblIns.Size = New System.Drawing.Size(188, 16)
Me.lblIns.TabIndex = 19
Me.lblIns.Text = "Insurance cost based on first year:"
'
'lblTax
'
Me.lblTax.Font = New System.Drawing.Font("Micro soft Sans Serif", 8.25!, System.Drawing.FontStyle.B old, System.Drawing.GraphicsUni t.Point, CType(0, Byte))
Me.lblTax.Location = New System.Drawing.Point(28, 136)
Me.lblTax.Name = "lblTax"
Me.lblTax.Size = New System.Drawing.Size(188, 16)
Me.lblTax.TabIndex = 20
Me.lblTax.Text = "Tax base on first year cost:"
'
'txtIns
'
Me.txtIns.Font = New System.Drawing.Font("Micro soft Sans Serif", 8.25!, System.Drawing.FontStyle.B old, System.Drawing.GraphicsUni t.Point, CType(0, Byte))
Me.txtIns.Location = New System.Drawing.Point(232, 104)
Me.txtIns.Name = "txtIns"
Me.txtIns.Size = New System.Drawing.Size(84, 20)
Me.txtIns.TabIndex = 21
Me.txtIns.Text = "txtIns"
Me.ToolTip1.SetToolTip(Me. txtIns, "Based on a yearly amount for the total years applied(estimated)")
'
'txtTAX
'
Me.txtTAX.Font = New System.Drawing.Font("Micro soft Sans Serif", 8.25!, System.Drawing.FontStyle.B old, System.Drawing.GraphicsUni t.Point, CType(0, Byte))
Me.txtTAX.Location = New System.Drawing.Point(232, 132)
Me.txtTAX.Name = "txtTAX"
Me.txtTAX.Size = New System.Drawing.Size(84, 20)
Me.txtTAX.TabIndex = 22
Me.txtTAX.Text = "txtTAX"
Me.ToolTip1.SetToolTip(Me. txtTAX, "Based on a yearly amount for the total years applied(estimated)")
'
'btnPrint
'
Me.btnPrint.Enabled = False
Me.btnPrint.Font = New System.Drawing.Font("Micro soft Sans Serif", 8.25!, System.Drawing.FontStyle.B old, System.Drawing.GraphicsUni t.Point, CType(0, Byte))
Me.btnPrint.Location = New System.Drawing.Point(264, 376)
Me.btnPrint.Name = "btnPrint"
Me.btnPrint.Size = New System.Drawing.Size(64, 24)
Me.btnPrint.TabIndex = 23
Me.btnPrint.Text = "&Print"
'
'frmAmort
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(340, 414)
Me.Controls.Add(Me.btnPrin t)
Me.Controls.Add(Me.txtTAX)
Me.Controls.Add(Me.txtIns)
Me.Controls.Add(Me.txtMont hs)
Me.Controls.Add(Me.txtPerc entage)
Me.Controls.Add(Me.txtCost )
Me.Controls.Add(Me.lblTax)
Me.Controls.Add(Me.lblIns)
Me.Controls.Add(Me.btnCanc el)
Me.Controls.Add(Me.btnSave )
Me.Controls.Add(Me.btnClea r)
Me.Controls.Add(Me.lvwOut)
Me.Controls.Add(Me.lblMont hs)
Me.Controls.Add(Me.lblPerc entage)
Me.Controls.Add(Me.lblCost )
Me.Controls.Add(Me.btnOK)
Me.Icon = CType(resources.GetObject( "$this.Ico n"), System.Drawing.Icon)
Me.Name = "frmAmort"
Me.Text = "Amortization Program"
Me.ResumeLayout(False)
End Sub
#End Region
<STAThread()> Public Shared Sub Main()
Application.Run(New frmAmort)
End Sub
Private Sub InitializeListview()
Cursor.Current = Cursors.WaitCursor
lvwOut.Columns.Add("Month" , 50, HorizontalAlignment.Center )
lvwOut.Columns.Add("Paymen t", 70, HorizontalAlignment.Right)
lvwOut.Columns.Add("Princi pal", 80, HorizontalAlignment.Right)
lvwOut.Columns.Add("Intere st", 80, HorizontalAlignment.Right)
lvwOut.View = View.Details
Cursor.Current = Cursors.Default
txtCost.Text = "200,000"
txtPercentage.Text = "6.0"
txtMonths.Text = "360"
txtIns.Text = "1,500"
txtTAX.Text = "2,500"
End Sub
Private Sub btnOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOK.Click
TestPPMT()
End Sub
Private Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
printPreView()
End Sub
Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
Me.Close()
End Sub
Private Sub btnClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClear.Click
lvwOut.Clear()
lvwOut.Columns.Add("Month" , 50, HorizontalAlignment.Center )
lvwOut.Columns.Add("Paymen t", 70, HorizontalAlignment.Right)
lvwOut.Columns.Add("Princi pal", 80, HorizontalAlignment.Right)
lvwOut.Columns.Add("Intere st", 80, HorizontalAlignment.Right)
txtCost.Focus()
sLineTemp = ""
sLine = Space(70)
End Sub
Sub TestPPMT()
Dim PVal As Double
Dim APR As Double
Dim MakeChart As Double
Dim FVal As Double
Dim Ins As Double
Dim Tax As Double
Dim Payment As Double
Dim Period As Double
Dim P, I As Double
Dim PayType As DueDate
Dim Fmt As String
Dim Response As MsgBoxResult
Dim iCounter As Short = 0
Dim iCount As Short
'******** Temp Vars **********
Dim sCount As String
Dim sPayment As String
Dim sP As String
Dim sI As String
Cursor.Current = Cursors.WaitCursor
Fmt = "###,###,##0.00" ' Define money format.
If txtIns.Text = "" Then
Ins = 0
Else
Ins = CDbl(Trim(txtIns.Text)) / 12
End If
If txtIns.Text = "" Then
Tax = 0
Else
Tax = CDbl(Trim(txtTAX.Text)) / 12
End If
FVal = 0 ' Usually 0 for a loan.
PVal = CDbl(Trim(txtCost.Text))
APR = CDbl(Trim(txtPercentage.Te xt))
If APR > 1 Then APR = APR / 100 ' Ensure proper form.
TotPmts = CDbl(Trim(txtMonths.Text))
Response = MsgBox("Do you make payments at the end of month?", MsgBoxStyle.YesNo)
If Response = MsgBoxResult.No Then
PayType = DueDate.BegOfPeriod
Else
PayType = DueDate.EndOfPeriod
End If
Payment = Math.Abs(-Pmt(APR / 12, TotPmts, PVal, FVal, PayType)) + (Ins + Tax)
Period = 1
For iCount = 1 To TotPmts
' If Period > 12 Then Exit For ' Show only first 12.
P = PPmt(APR / 12, Period, TotPmts, -PVal, FVal, PayType)
P = (Int((P + 0.005) * 100) / 100) ' Round principal.
I = Payment - P
I = (Int((I + 0.005) * 100) / 100) ' Round interest.
sCount = Format(iCount, " 00")
sCount = Space(5 - Len(sCount)) & sCount
Payment = Format(Payment, Fmt)
sPayment = Payment
sPayment = Space(15 - Len(sPayment)) & sPayment
sP = Format(P, Fmt)
sP = Space(15 - Len(sP)) & sP
sI = Format(I, Fmt)
sI = Space(15 - Len(sI)) & sI
Mid(sLine, 5, 5) = sCount
Mid(sLine, 15, 30) = sPayment
Mid(sLine, 35, 50) = sP
Mid(sLine, 55, 70) = sI
sLineTemp &= sLine & vbNewLine
sCount = ""
sPayment = ""
sP = ""
sI = ""
lvwOut.Items.Add(UCase(iCo unt))
lvwOut.Items(iCounter).Sub Items.Add( Payment) 'Format(Payment, Fmt))
lvwOut.Items(iCounter).Sub Items.Add( Format(P, Fmt))
lvwOut.Items(iCounter).Sub Items.Add( Format(I, Fmt))
'Add each line to a collection for recalling later
' cCollection.Add(Msg, iCounter)
iCounter += 1
Period += 1
Next
Mid(sHeader, 5, 5) = "Month"
Mid(sHeader, 20, 35) = "Payment"
Mid(sHeader, 40, 55) = "Principal"
Mid(sHeader, 60, 80) = "Interest " & Now
sHeader &= vbNewLine
sHeader1 = ("________________________ __________ __________ __________ __________ ________")
sHeader1 &= vbNewLine
Cursor.Current = Cursors.Default
End Sub
Private Sub txtCost_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPr essEventAr gs) Handles txtCost.KeyPress, txtMonths.KeyPress, txtPercentage.KeyPress
Dim KeyAscii As Short = Asc(e.KeyChar)
Select Case KeyAscii
Case System.Windows.Forms.Keys. Back
Case 13
e.Handled = True
SendKeys.Send("{TAB}")
Case Is >= 32
KeyAscii = 0
Case 48 To 57, 8
Exit Sub
Case Else
e.Handled = True
End Select
End Sub
Public Sub printPreView()
If sFileName = "" Then
OpenFile()
End If
Try
Dim sWE As String
sWE = sHeader & Msg
Dim streamToPrint As StreamReader = New StreamReader(sFileName)
Try
'Assumes the default printer
Dim pd As TextFilePrintDocument = New TextFilePrintDocument(stre amToPrint)
If Not (storedPageSettings Is Nothing) Then
pd.DefaultPageSettings = storedPageSettings
End If
Dim dlg As New PrintPreviewDialog
dlg.Document = pd
dlg.ShowDialog()
Finally
streamToPrint.Close()
End Try
Catch exp As Exception
MessageBox.Show("An error occurred - " + exp.Message)
End Try
End Sub
Private Sub OpenFile()
' Create the OpenFileDialog object
Dim myOpenFileDialog As New OpenFileDialog
With myOpenFileDialog
.CheckFileExists = True
.DefaultExt = "txt"
.Filter = "Text files (*.txt)|*.txt"
.InitialDirectory = "c:\Amortization\Reports\"
.Multiselect = False
End With
' Use the OpenFileDialog and put the path and name of the selected file in a var.
If myOpenFileDialog.ShowDialo g = DialogResult.OK Then
sFileName = myOpenFileDialog.FileName
End If
End Sub
Private Sub SaveNewFileName(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
' Use the SaveFileDialog and put the path and name of the
' desired file in a var.
' Create the OpenFileDialog object
Dim mySaveFileDialog As New SaveFileDialog
With mySaveFileDialog
.DefaultExt = "txt"
.Filter = "Text files (*.txt)|*.txt"
.InitialDirectory = "c:\Amortization\Reports\"
sFileName = .FileName
End With
If mySaveFileDialog.ShowDialo g = DialogResult.OK Then
With mySaveFileDialog
sFileName = .FileName
End With
If sFileName <> "" Then
StreamWriterCREATEFile(sFi leName, (sHeader & sHeader1 & sLineTemp))
btnPrint.Enabled = True
End If
End If
End Sub
Public Sub StreamWriterCREATEFile(ByV al sFileName As String, ByVal sData As String)
Dim myStreamWriter As StreamWriter
Dim Temp As String
Dim i As Short
Try
myStreamWriter = File.CreateText(sFileName)
For i = 0 To TotPmts + 1
Temp = Microsoft.VisualBasic.Mid( sData, 2, InStr(sData, vbNewLine) - 2)
sData = Mid(sData, InStr(sData, vbNewLine) + 1)
myStreamWriter.WriteLine(T emp)
Select Case i
Case 56, 111, 166, 221, 276, 331, 386, 441, 496, 551, 606, 661
myStreamWriter.WriteLine(M icrosoft.V isualBasic .Mid(sHead er, 2, InStr(sHeader, vbNewLine) - 2))
myStreamWriter.WriteLine(M icrosoft.V isualBasic .Mid(sHead er1, 2, InStr(sHeader1, vbNewLine) - 2))
End Select
Next
myStreamWriter.Flush()
Catch exc As Exception
MsgBox("File could not be created or written to." + vbCrLf + _
"Please verify that the filename is correct, " + _
"and that you have write permissions for the desired " + _
"directory." + vbCrLf + vbCrLf + "Exception: " + exc.Message)
Finally
' Close the object if it has been created.
If Not myStreamWriter Is Nothing Then
myStreamWriter.Close()
End If
End Try
End Sub
End Class
'PRINT CLASS
Imports System
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Printing
Imports System.IO
Public Class TextFilePrintDocument
Inherits PrintDocument
Private printFont As Font
Private streamToPrint As StreamReader
Public Sub New(ByVal streamToPrint As StreamReader)
MyBase.New()
Me.streamToPrint = streamToPrint
End Sub
'Override OnBeginPrint to set up the font we are going to use
Protected Overrides Sub OnBeginPrint(ByVal ev As PrintEventArgs)
MyBase.OnBeginPrint(ev)
printFont = New Font("Arial", 10)
End Sub
'Override the OnPrintPage to provide the printing logic for the document
Protected Overrides Sub OnPrintPage(ByVal ev As PrintPageEventArgs)
MyBase.OnPrintPage(ev)
Dim lpp As Single = 0
Dim yPos As Single = 0
Dim count As Integer = 0
Dim leftMargin As Single = ev.MarginBounds.Left
Dim topMargin As Single = ev.MarginBounds.Top
Dim line As String
'Work out the number of lines per page
'Use the MarginBounds on the event to do this
lpp = ev.MarginBounds.Height / printFont.GetHeight(ev.Gra phics)
'Now iterate over the file printing out each line
'NOTE WELL: This assumes that a single line is not wider than the page width
'Check count first so that we don't read line that we won't print
line = streamToPrint.ReadLine()
While (count < lpp And line <> Nothing)
yPos = topMargin + (count * printFont.GetHeight(ev.Gra phics))
ev.Graphics.DrawString(lin e, printFont, Brushes.Black, leftMargin, _
yPos, New StringFormat)
count = count + 1
If (count < lpp) Then
line = streamToPrint.ReadLine()
End If
End While
'If we have more lines then print another page
If (line <> Nothing) Then
ev.HasMorePages = True
Else
ev.HasMorePages = False
End If
End Sub
End Class
'FORM 1
Imports System.IO
Imports System
Imports System.Drawing.Printing
Imports Microsoft.VisualBasic
Public Class frmAmort
Inherits System.Windows.Forms.Form
Private WithEvents pdoc As New PrintDocument
Private cCollection As New Collection
Private Msg As String
Private sLineTemp As String
Private sFileName As String
Private sLine As String = Space(70)
Private sHeader As String = Space(90)
Private sHeader1 As String = Space(90)
Private printFont As Font
Private streamToPrint As StreamReader
Private storedPageSettings As PageSettings
Private TotPmts As Double
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
InitializeListview()
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.ICon
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents lblCost As System.Windows.Forms.Label
Friend WithEvents txtCost As System.Windows.Forms.TextB
Friend WithEvents btnOK As System.Windows.Forms.Butto
Friend WithEvents txtPercentage As System.Windows.Forms.TextB
Friend WithEvents txtMonths As System.Windows.Forms.TextB
Friend WithEvents lvwOut As System.Windows.Forms.ListV
Friend WithEvents btnClear As System.Windows.Forms.Butto
Friend WithEvents lblPercentage As System.Windows.Forms.Label
Friend WithEvents lblMonths As System.Windows.Forms.Label
Friend WithEvents btnSave As System.Windows.Forms.Butto
Friend WithEvents btnCancel As System.Windows.Forms.Butto
Friend WithEvents lblIns As System.Windows.Forms.Label
Friend WithEvents txtIns As System.Windows.Forms.TextB
Friend WithEvents txtTAX As System.Windows.Forms.TextB
Friend WithEvents btnPrint As System.Windows.Forms.Butto
Friend WithEvents lblTax As System.Windows.Forms.Label
Friend WithEvents ToolTip1 As System.Windows.Forms.ToolT
<System.Diagnostics.Debugg
Me.components = New System.ComponentModel.Cont
Dim resources As System.Resources.ResourceM
Me.btnOK = New System.Windows.Forms.Butto
Me.lblCost = New System.Windows.Forms.Label
Me.lblPercentage = New System.Windows.Forms.Label
Me.lblMonths = New System.Windows.Forms.Label
Me.txtCost = New System.Windows.Forms.TextB
Me.txtPercentage = New System.Windows.Forms.TextB
Me.txtMonths = New System.Windows.Forms.TextB
Me.lvwOut = New System.Windows.Forms.ListV
Me.btnClear = New System.Windows.Forms.Butto
Me.btnSave = New System.Windows.Forms.Butto
Me.btnCancel = New System.Windows.Forms.Butto
Me.lblIns = New System.Windows.Forms.Label
Me.lblTax = New System.Windows.Forms.Label
Me.txtIns = New System.Windows.Forms.TextB
Me.txtTAX = New System.Windows.Forms.TextB
Me.btnPrint = New System.Windows.Forms.Butto
Me.ToolTip1 = New System.Windows.Forms.ToolT
Me.SuspendLayout()
'
'btnOK
'
Me.btnOK.Font = New System.Drawing.Font("Micro
Me.btnOK.Location = New System.Drawing.Point(8, 376)
Me.btnOK.Name = "btnOK"
Me.btnOK.Size = New System.Drawing.Size(60, 24)
Me.btnOK.TabIndex = 3
Me.btnOK.Text = "&Ok"
Me.ToolTip1.SetToolTip(Me.
'
'lblCost
'
Me.lblCost.Font = New System.Drawing.Font("Micro
Me.lblCost.Location = New System.Drawing.Point(28, 8)
Me.lblCost.Name = "lblCost"
Me.lblCost.Size = New System.Drawing.Size(200, 20)
Me.lblCost.TabIndex = 4
Me.lblCost.Text = "How much do you want to borrow? :"
'
'lblPercentage
'
Me.lblPercentage.Font = New System.Drawing.Font("Micro
Me.lblPercentage.Location = New System.Drawing.Point(28, 36)
Me.lblPercentage.Name = "lblPercentage"
Me.lblPercentage.Size = New System.Drawing.Size(196, 28)
Me.lblPercentage.TabIndex = 5
Me.lblPercentage.Text = "What is the annual percentage rate of your loan? :"
'
'lblMonths
'
Me.lblMonths.Font = New System.Drawing.Font("Micro
Me.lblMonths.Location = New System.Drawing.Point(28, 72)
Me.lblMonths.Name = "lblMonths"
Me.lblMonths.Size = New System.Drawing.Size(192, 28)
Me.lblMonths.TabIndex = 6
Me.lblMonths.Text = "How many monthly payments do you have to make? :"
'
'txtCost
'
Me.txtCost.Font = New System.Drawing.Font("Micro
Me.txtCost.Location = New System.Drawing.Point(232, 8)
Me.txtCost.Name = "txtCost"
Me.txtCost.Size = New System.Drawing.Size(84, 20)
Me.txtCost.TabIndex = 0
Me.txtCost.Text = "txtCost"
'
'txtPercentage
'
Me.txtPercentage.Font = New System.Drawing.Font("Micro
Me.txtPercentage.Location = New System.Drawing.Point(232, 40)
Me.txtPercentage.Name = "txtPercentage"
Me.txtPercentage.Size = New System.Drawing.Size(84, 20)
Me.txtPercentage.TabIndex = 1
Me.txtPercentage.Text = "txtPercentage"
'
'txtMonths
'
Me.txtMonths.Font = New System.Drawing.Font("Micro
Me.txtMonths.Location = New System.Drawing.Point(232, 76)
Me.txtMonths.Name = "txtMonths"
Me.txtMonths.Size = New System.Drawing.Size(84, 20)
Me.txtMonths.TabIndex = 2
Me.txtMonths.Text = "txtMonths"
'
'lvwOut
'
Me.lvwOut.Font = New System.Drawing.Font("Micro
Me.lvwOut.Location = New System.Drawing.Point(12, 164)
Me.lvwOut.Name = "lvwOut"
Me.lvwOut.Size = New System.Drawing.Size(316, 200)
Me.lvwOut.TabIndex = 12
'
'btnClear
'
Me.btnClear.Font = New System.Drawing.Font("Micro
Me.btnClear.Location = New System.Drawing.Point(136, 376)
Me.btnClear.Name = "btnClear"
Me.btnClear.Size = New System.Drawing.Size(64, 24)
Me.btnClear.TabIndex = 15
Me.btnClear.Text = "&Clear"
Me.ToolTip1.SetToolTip(Me.
'
'btnSave
'
Me.btnSave.Font = New System.Drawing.Font("Micro
Me.btnSave.Location = New System.Drawing.Point(200, 376)
Me.btnSave.Name = "btnSave"
Me.btnSave.Size = New System.Drawing.Size(64, 24)
Me.btnSave.TabIndex = 17
Me.btnSave.Text = "&Save"
Me.ToolTip1.SetToolTip(Me.
'
'btnCancel
'
Me.btnCancel.Font = New System.Drawing.Font("Micro
Me.btnCancel.Location = New System.Drawing.Point(68, 376)
Me.btnCancel.Name = "btnCancel"
Me.btnCancel.Size = New System.Drawing.Size(68, 24)
Me.btnCancel.TabIndex = 18
Me.btnCancel.Text = "C&ancel"
Me.ToolTip1.SetToolTip(Me.
'
'lblIns
'
Me.lblIns.Font = New System.Drawing.Font("Micro
Me.lblIns.Location = New System.Drawing.Point(28, 108)
Me.lblIns.Name = "lblIns"
Me.lblIns.Size = New System.Drawing.Size(188, 16)
Me.lblIns.TabIndex = 19
Me.lblIns.Text = "Insurance cost based on first year:"
'
'lblTax
'
Me.lblTax.Font = New System.Drawing.Font("Micro
Me.lblTax.Location = New System.Drawing.Point(28, 136)
Me.lblTax.Name = "lblTax"
Me.lblTax.Size = New System.Drawing.Size(188, 16)
Me.lblTax.TabIndex = 20
Me.lblTax.Text = "Tax base on first year cost:"
'
'txtIns
'
Me.txtIns.Font = New System.Drawing.Font("Micro
Me.txtIns.Location = New System.Drawing.Point(232, 104)
Me.txtIns.Name = "txtIns"
Me.txtIns.Size = New System.Drawing.Size(84, 20)
Me.txtIns.TabIndex = 21
Me.txtIns.Text = "txtIns"
Me.ToolTip1.SetToolTip(Me.
'
'txtTAX
'
Me.txtTAX.Font = New System.Drawing.Font("Micro
Me.txtTAX.Location = New System.Drawing.Point(232, 132)
Me.txtTAX.Name = "txtTAX"
Me.txtTAX.Size = New System.Drawing.Size(84, 20)
Me.txtTAX.TabIndex = 22
Me.txtTAX.Text = "txtTAX"
Me.ToolTip1.SetToolTip(Me.
'
'btnPrint
'
Me.btnPrint.Enabled = False
Me.btnPrint.Font = New System.Drawing.Font("Micro
Me.btnPrint.Location = New System.Drawing.Point(264, 376)
Me.btnPrint.Name = "btnPrint"
Me.btnPrint.Size = New System.Drawing.Size(64, 24)
Me.btnPrint.TabIndex = 23
Me.btnPrint.Text = "&Print"
'
'frmAmort
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(340, 414)
Me.Controls.Add(Me.btnPrin
Me.Controls.Add(Me.txtTAX)
Me.Controls.Add(Me.txtIns)
Me.Controls.Add(Me.txtMont
Me.Controls.Add(Me.txtPerc
Me.Controls.Add(Me.txtCost
Me.Controls.Add(Me.lblTax)
Me.Controls.Add(Me.lblIns)
Me.Controls.Add(Me.btnCanc
Me.Controls.Add(Me.btnSave
Me.Controls.Add(Me.btnClea
Me.Controls.Add(Me.lvwOut)
Me.Controls.Add(Me.lblMont
Me.Controls.Add(Me.lblPerc
Me.Controls.Add(Me.lblCost
Me.Controls.Add(Me.btnOK)
Me.Icon = CType(resources.GetObject(
Me.Name = "frmAmort"
Me.Text = "Amortization Program"
Me.ResumeLayout(False)
End Sub
#End Region
<STAThread()> Public Shared Sub Main()
Application.Run(New frmAmort)
End Sub
Private Sub InitializeListview()
Cursor.Current = Cursors.WaitCursor
lvwOut.Columns.Add("Month"
lvwOut.Columns.Add("Paymen
lvwOut.Columns.Add("Princi
lvwOut.Columns.Add("Intere
lvwOut.View = View.Details
Cursor.Current = Cursors.Default
txtCost.Text = "200,000"
txtPercentage.Text = "6.0"
txtMonths.Text = "360"
txtIns.Text = "1,500"
txtTAX.Text = "2,500"
End Sub
Private Sub btnOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOK.Click
TestPPMT()
End Sub
Private Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
printPreView()
End Sub
Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
Me.Close()
End Sub
Private Sub btnClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClear.Click
lvwOut.Clear()
lvwOut.Columns.Add("Month"
lvwOut.Columns.Add("Paymen
lvwOut.Columns.Add("Princi
lvwOut.Columns.Add("Intere
txtCost.Focus()
sLineTemp = ""
sLine = Space(70)
End Sub
Sub TestPPMT()
Dim PVal As Double
Dim APR As Double
Dim MakeChart As Double
Dim FVal As Double
Dim Ins As Double
Dim Tax As Double
Dim Payment As Double
Dim Period As Double
Dim P, I As Double
Dim PayType As DueDate
Dim Fmt As String
Dim Response As MsgBoxResult
Dim iCounter As Short = 0
Dim iCount As Short
'******** Temp Vars **********
Dim sCount As String
Dim sPayment As String
Dim sP As String
Dim sI As String
Cursor.Current = Cursors.WaitCursor
Fmt = "###,###,##0.00" ' Define money format.
If txtIns.Text = "" Then
Ins = 0
Else
Ins = CDbl(Trim(txtIns.Text)) / 12
End If
If txtIns.Text = "" Then
Tax = 0
Else
Tax = CDbl(Trim(txtTAX.Text)) / 12
End If
FVal = 0 ' Usually 0 for a loan.
PVal = CDbl(Trim(txtCost.Text))
APR = CDbl(Trim(txtPercentage.Te
If APR > 1 Then APR = APR / 100 ' Ensure proper form.
TotPmts = CDbl(Trim(txtMonths.Text))
Response = MsgBox("Do you make payments at the end of month?", MsgBoxStyle.YesNo)
If Response = MsgBoxResult.No Then
PayType = DueDate.BegOfPeriod
Else
PayType = DueDate.EndOfPeriod
End If
Payment = Math.Abs(-Pmt(APR / 12, TotPmts, PVal, FVal, PayType)) + (Ins + Tax)
Period = 1
For iCount = 1 To TotPmts
' If Period > 12 Then Exit For ' Show only first 12.
P = PPmt(APR / 12, Period, TotPmts, -PVal, FVal, PayType)
P = (Int((P + 0.005) * 100) / 100) ' Round principal.
I = Payment - P
I = (Int((I + 0.005) * 100) / 100) ' Round interest.
sCount = Format(iCount, " 00")
sCount = Space(5 - Len(sCount)) & sCount
Payment = Format(Payment, Fmt)
sPayment = Payment
sPayment = Space(15 - Len(sPayment)) & sPayment
sP = Format(P, Fmt)
sP = Space(15 - Len(sP)) & sP
sI = Format(I, Fmt)
sI = Space(15 - Len(sI)) & sI
Mid(sLine, 5, 5) = sCount
Mid(sLine, 15, 30) = sPayment
Mid(sLine, 35, 50) = sP
Mid(sLine, 55, 70) = sI
sLineTemp &= sLine & vbNewLine
sCount = ""
sPayment = ""
sP = ""
sI = ""
lvwOut.Items.Add(UCase(iCo
lvwOut.Items(iCounter).Sub
lvwOut.Items(iCounter).Sub
lvwOut.Items(iCounter).Sub
'Add each line to a collection for recalling later
' cCollection.Add(Msg, iCounter)
iCounter += 1
Period += 1
Next
Mid(sHeader, 5, 5) = "Month"
Mid(sHeader, 20, 35) = "Payment"
Mid(sHeader, 40, 55) = "Principal"
Mid(sHeader, 60, 80) = "Interest " & Now
sHeader &= vbNewLine
sHeader1 = ("________________________
sHeader1 &= vbNewLine
Cursor.Current = Cursors.Default
End Sub
Private Sub txtCost_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPr
Dim KeyAscii As Short = Asc(e.KeyChar)
Select Case KeyAscii
Case System.Windows.Forms.Keys.
Case 13
e.Handled = True
SendKeys.Send("{TAB}")
Case Is >= 32
KeyAscii = 0
Case 48 To 57, 8
Exit Sub
Case Else
e.Handled = True
End Select
End Sub
Public Sub printPreView()
If sFileName = "" Then
OpenFile()
End If
Try
Dim sWE As String
sWE = sHeader & Msg
Dim streamToPrint As StreamReader = New StreamReader(sFileName)
Try
'Assumes the default printer
Dim pd As TextFilePrintDocument = New TextFilePrintDocument(stre
If Not (storedPageSettings Is Nothing) Then
pd.DefaultPageSettings = storedPageSettings
End If
Dim dlg As New PrintPreviewDialog
dlg.Document = pd
dlg.ShowDialog()
Finally
streamToPrint.Close()
End Try
Catch exp As Exception
MessageBox.Show("An error occurred - " + exp.Message)
End Try
End Sub
Private Sub OpenFile()
' Create the OpenFileDialog object
Dim myOpenFileDialog As New OpenFileDialog
With myOpenFileDialog
.CheckFileExists = True
.DefaultExt = "txt"
.Filter = "Text files (*.txt)|*.txt"
.InitialDirectory = "c:\Amortization\Reports\"
.Multiselect = False
End With
' Use the OpenFileDialog and put the path and name of the selected file in a var.
If myOpenFileDialog.ShowDialo
sFileName = myOpenFileDialog.FileName
End If
End Sub
Private Sub SaveNewFileName(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
' Use the SaveFileDialog and put the path and name of the
' desired file in a var.
' Create the OpenFileDialog object
Dim mySaveFileDialog As New SaveFileDialog
With mySaveFileDialog
.DefaultExt = "txt"
.Filter = "Text files (*.txt)|*.txt"
.InitialDirectory = "c:\Amortization\Reports\"
sFileName = .FileName
End With
If mySaveFileDialog.ShowDialo
With mySaveFileDialog
sFileName = .FileName
End With
If sFileName <> "" Then
StreamWriterCREATEFile(sFi
btnPrint.Enabled = True
End If
End If
End Sub
Public Sub StreamWriterCREATEFile(ByV
Dim myStreamWriter As StreamWriter
Dim Temp As String
Dim i As Short
Try
myStreamWriter = File.CreateText(sFileName)
For i = 0 To TotPmts + 1
Temp = Microsoft.VisualBasic.Mid(
sData = Mid(sData, InStr(sData, vbNewLine) + 1)
myStreamWriter.WriteLine(T
Select Case i
Case 56, 111, 166, 221, 276, 331, 386, 441, 496, 551, 606, 661
myStreamWriter.WriteLine(M
myStreamWriter.WriteLine(M
End Select
Next
myStreamWriter.Flush()
Catch exc As Exception
MsgBox("File could not be created or written to." + vbCrLf + _
"Please verify that the filename is correct, " + _
"and that you have write permissions for the desired " + _
"directory." + vbCrLf + vbCrLf + "Exception: " + exc.Message)
Finally
' Close the object if it has been created.
If Not myStreamWriter Is Nothing Then
myStreamWriter.Close()
End If
End Try
End Sub
End Class
'PRINT CLASS
Imports System
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Printing
Imports System.IO
Public Class TextFilePrintDocument
Inherits PrintDocument
Private printFont As Font
Private streamToPrint As StreamReader
Public Sub New(ByVal streamToPrint As StreamReader)
MyBase.New()
Me.streamToPrint = streamToPrint
End Sub
'Override OnBeginPrint to set up the font we are going to use
Protected Overrides Sub OnBeginPrint(ByVal ev As PrintEventArgs)
MyBase.OnBeginPrint(ev)
printFont = New Font("Arial", 10)
End Sub
'Override the OnPrintPage to provide the printing logic for the document
Protected Overrides Sub OnPrintPage(ByVal ev As PrintPageEventArgs)
MyBase.OnPrintPage(ev)
Dim lpp As Single = 0
Dim yPos As Single = 0
Dim count As Integer = 0
Dim leftMargin As Single = ev.MarginBounds.Left
Dim topMargin As Single = ev.MarginBounds.Top
Dim line As String
'Work out the number of lines per page
'Use the MarginBounds on the event to do this
lpp = ev.MarginBounds.Height / printFont.GetHeight(ev.Gra
'Now iterate over the file printing out each line
'NOTE WELL: This assumes that a single line is not wider than the page width
'Check count first so that we don't read line that we won't print
line = streamToPrint.ReadLine()
While (count < lpp And line <> Nothing)
yPos = topMargin + (count * printFont.GetHeight(ev.Gra
ev.Graphics.DrawString(lin
yPos, New StringFormat)
count = count + 1
If (count < lpp) Then
line = streamToPrint.ReadLine()
End If
End While
'If we have more lines then print another page
If (line <> Nothing) Then
ev.HasMorePages = True
Else
ev.HasMorePages = False
End If
End Sub
End Class
ASKER
Hey planocz, that code seems interesting, but I can't figure out how to actually computer the monthly payment. Your code seems way more complex then what I'm trying to do. I need to calculate my monthly payment give the loan principal, the percentage, and the terms. Here are my variables.
dblPrincipal = CDbl(txtPrincipal.Text)
dblRate = CDbl(txtRate.Text)
dblYears = CDbl(txtYears.Text)
dblPrincipal = CDbl(txtPrincipal.Text)
dblRate = CDbl(txtRate.Text)
dblYears = CDbl(txtYears.Text)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Can't someone just type in the formua as I would use it in a VB app? I didn't post this question so I could go on a scavanger hunt.
I'm sorry, I appreciate your help, and I'm not blaming you personally. I've posted 2 questions already for basically the same issue. I guess it's time to post a 3rd.
I'm sorry, I appreciate your help, and I'm not blaming you personally. I've posted 2 questions already for basically the same issue. I guess it's time to post a 3rd.
All the code that is posted is a simplest way to program what you want with the tools that Micro$oft provide in .NET.
Ok, well i dont exactly get what u mean by mortgage here, but u can try the following:
1. If u want to calculate the Total Amount to be paid after the given number of years, do this:
dblAmount = dblPrincipal + (dblPrincipal * dblRate * dblYears) / 100.0
2. If u just want the Interest Amount after the given number of years, do this:
dblInterest = (dblPrincipal * dblRate * dblYears) / 100.0
hope this helps.
1. If u want to calculate the Total Amount to be paid after the given number of years, do this:
dblAmount = dblPrincipal + (dblPrincipal * dblRate * dblYears) / 100.0
2. If u just want the Interest Amount after the given number of years, do this:
dblInterest = (dblPrincipal * dblRate * dblYears) / 100.0
hope this helps.
http://www.codeproject.com/netcf/BNMortCalc.asp
It is written in Compact Framework and C# but it should have the basic gist on how to create one. Cheers.