' Student: Pat Moss ' Instructor: Greg Simpson ' CIS259, Sect. 5844, VB.NET II ' January 24, 2006 Tuesday 5:40 pm ' Chapter 02, Project 05, p. 122-123 ' Textbook: Visual Basic .NET by Michael Ekedahl, 0619213353 ' This form is: frmAmort.vb Rev. 01c Amortization Schedule screen ' Software Specification: ' Design an Amortization Schedule, including a Startup ' Sub Main procedure, a Splash screen, a User Input Screen, ' the Amortization Schedule screen, and Menu controls. Option Strict On Option Explicit On Public Class frmAmort Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New(ByVal dblBegPrin As Double, ByVal dblAddPymt As Double, ByVal intPeriods As Integer, ByVal sngIntRate As Single) MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call ' Dynamically create the rows/columns for the Amortization Schedule Call ConfigureRows(intPeriods) ' Calculate and Populate the Amortization Schedule Call CalculateAmort() 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.IContainer '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 btnClose As System.Windows.Forms.Button Friend WithEvents pnlItems As System.Windows.Forms.Panel Friend WithEvents txtIntRate As System.Windows.Forms.TextBox Friend WithEvents txtInitAmt As System.Windows.Forms.TextBox Friend WithEvents lblhdrIntRate As System.Windows.Forms.Label Friend WithEvents lblhdrInitAmt As System.Windows.Forms.Label Private Sub InitializeComponent() Me.btnClose = New System.Windows.Forms.Button Me.pnlItems = New System.Windows.Forms.Panel Me.txtIntRate = New System.Windows.Forms.TextBox Me.txtInitAmt = New System.Windows.Forms.TextBox Me.lblhdrIntRate = New System.Windows.Forms.Label Me.lblhdrInitAmt = New System.Windows.Forms.Label Me.SuspendLayout() ' 'btnClose ' Me.btnClose.FlatStyle = System.Windows.Forms.FlatStyle.Popup Me.btnClose.Location = New System.Drawing.Point(328, 24) Me.btnClose.Name = "btnClose" Me.btnClose.Size = New System.Drawing.Size(72, 32) Me.btnClose.TabIndex = 0 Me.btnClose.Text = "&Close" ' 'pnlItems ' Me.pnlItems.AutoScroll = True Me.pnlItems.Dock = System.Windows.Forms.DockStyle.Bottom Me.pnlItems.Location = New System.Drawing.Point(0, 66) Me.pnlItems.Name = "pnlItems" Me.pnlItems.Size = New System.Drawing.Size(456, 300) Me.pnlItems.TabIndex = 3 ' 'txtIntRate ' Me.txtIntRate.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.txtIntRate.Location = New System.Drawing.Point(160, 40) Me.txtIntRate.Name = "txtIntRate" Me.txtIntRate.Size = New System.Drawing.Size(152, 26) Me.txtIntRate.TabIndex = 9 Me.txtIntRate.Text = "" ' 'txtInitAmt ' Me.txtInitAmt.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.txtInitAmt.Location = New System.Drawing.Point(160, 8) Me.txtInitAmt.Name = "txtInitAmt" Me.txtInitAmt.Size = New System.Drawing.Size(152, 26) Me.txtInitAmt.TabIndex = 8 Me.txtInitAmt.Text = "" ' 'lblhdrIntRate ' Me.lblhdrIntRate.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.lblhdrIntRate.ImageAlign = System.Drawing.ContentAlignment.MiddleRight Me.lblhdrIntRate.Location = New System.Drawing.Point(8, 40) Me.lblhdrIntRate.Name = "lblhdrIntRate" Me.lblhdrIntRate.Size = New System.Drawing.Size(136, 23) Me.lblhdrIntRate.TabIndex = 7 Me.lblhdrIntRate.Text = "Interest Amount:" Me.lblhdrIntRate.TextAlign = System.Drawing.ContentAlignment.MiddleRight ' 'lblhdrInitAmt ' Me.lblhdrInitAmt.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.lblhdrInitAmt.ImageAlign = System.Drawing.ContentAlignment.MiddleRight Me.lblhdrInitAmt.Location = New System.Drawing.Point(8, 8) Me.lblhdrInitAmt.Name = "lblhdrInitAmt" Me.lblhdrInitAmt.Size = New System.Drawing.Size(136, 23) Me.lblhdrInitAmt.TabIndex = 6 Me.lblhdrInitAmt.Text = "Initial Amount:" Me.lblhdrInitAmt.TextAlign = System.Drawing.ContentAlignment.MiddleRight ' 'frmAmort ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.AutoScroll = True Me.ClientSize = New System.Drawing.Size(456, 366) Me.Controls.Add(Me.txtIntRate) Me.Controls.Add(Me.txtInitAmt) Me.Controls.Add(Me.lblhdrIntRate) Me.Controls.Add(Me.lblhdrInitAmt) Me.Controls.Add(Me.pnlItems) Me.Controls.Add(Me.btnClose) Me.Name = "frmAmort" Me.ShowInTaskbar = False Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen Me.Text = "Chap 2, Proj 5 Amortization Schedule by Pat Moss" Me.ResumeLayout(False) End Sub #End Region ' Declare one dynamic array to store the created ' Label control instances for: ' lblPeriod, lblInterest, lblAmtandInt, lblPayment, lblEndBal ' control instances. The array named lblItemRows is a 2-dimensional ' array. Private lblItemRows(,) As System.Windows.Forms.Label ' The following constants improve readability by defining the ' standard size of each label box. Private Const cintItemMax As Integer = 5 Private Const cintItemBoxHeight As Integer = 15 Private Const cintItemBoxWidth As Integer = 70 rem Private Const cintItem1BoxWidth As Integer = 20 rem Private Const cintLabel2Width As Integer = 100 rem Private Const cintLabel3Width As Integer = 200 rem Private Const cintLabel4Width As Integer = 200 rem Private Const cintLabel5Width As Integer = 200 ' pntCurrent is used to create a Point structure. Private pntCurrent As System.Drawing.Point ' All of the work takes place in the ConfigureRows procedure. This ' procedure is called by the constructor. See the New procedure in ' the Windows Form Designer generated code. Private Sub ConfigureRows(ByVal InpRowCount As Integer) ' Variables to store the current item and the current row. Dim pintItemCurrent, pintRowCurrent As Integer ReDim lblItemRows(cintItemMax, InpRowCount + 1) For pintItemCurrent = 0 To cintItemMax - 1 For pintRowCurrent = 0 To InpRowCount Dim intItemWidth As Integer rem If (pintItemCurrent = -1) Then rem intItemWidth = cintItem1BoxWidth rem Else rem intItemWidth = cintItemBoxWidth rem End If intItemWidth = cintItemBoxWidth pntCurrent = New Point(30 + (pintItemCurrent + 0) * (intItemWidth + 5), _ (pintRowCurrent + 1) * cintItemBoxHeight) Call CreateItemRows(pintItemCurrent, pintRowCurrent, pntCurrent) If ((pintRowCurrent = 0) And (pintItemCurrent = 4)) Then lblItemRows(0, pintRowCurrent).Text = "Period" lblItemRows(1, pintRowCurrent).Text = "Interest" lblItemRows(2, pintRowCurrent).Text = "Amt w/Int" lblItemRows(3, pintRowCurrent).Text = "Pymt Amt" lblItemRows(4, pintRowCurrent).Text = "Balance" End If Next Next End Sub ' Dynamically create the rows/columns for the Amortization Schedule. ' The CreateItemRows procedure is responsible for actually creating ' each Label control instance and adding a reference to the array. ' The current item and row are passed as arguments, along with the ' position of the label control. Private Sub CreateItemRows(ByVal pintItemCurrent As Integer, ByVal pintRowCurrent As Integer, ByVal pnt As Point) ' Create an instance of the Label control and make it visible. lblItemRows(pintItemCurrent, pintRowCurrent) = New Label() lblItemRows(pintItemCurrent, pintRowCurrent).Visible = True lblItemRows(pintItemCurrent, pintRowCurrent).Text = _ (pintRowCurrent + 1).ToString() If (pintRowCurrent <> 0) Then lblItemRows(pintItemCurrent, pintRowCurrent).BackColor = Color.White End If lblItemRows(pintItemCurrent, pintRowCurrent).TextAlign = ContentAlignment.MiddleCenter ' Define the size of the Label control instance by creating an ' instance of the Size structure and assigning a value to the Size ' property. lblItemRows(pintItemCurrent, pintRowCurrent).Size = _ New System.Drawing.Size(cintItemBoxWidth, cintItemBoxHeight) ' Define the position of the Label control instance. lblItemRows(pintItemCurrent, pintRowCurrent).Location = pnt ' Finally, add the newly created Label control instance to the Controls ' collection for the Panel. Note that by adding the control instance to the ' Controls collection of the Panel rather than the form, the control instances ' will be contained by the Panel. The reason is simple. The Label control ' instances will scroll with the Panel instead of the form. Me.pnlItems.Controls.Add(lblItemRows(pintItemCurrent, pintRowCurrent)) End Sub Dim intPeriodsMax As Integer = glintPeriods Dim sngIntRate As Single = glsngIntRate Dim intPeriod As Integer = 0 Dim dblBegBal As Double = 0 Dim dblInterest As Double = 0 Dim dblAmtandInt As Double = 0 Dim dblPayment As Double = gldblAddPymt Dim dblEndBal As Double = gldblBegPrin ' Calculate and Populate the Amortization Schedule Private Sub CalculateAmort() Dim pintRowCurrent As Integer = 0 For pintRowCurrent = 1 To glintPeriods dblBegBal = dblEndBal dblInterest = dblBegBal * sngIntRate dblAmtandInt = dblBegBal + dblInterest dblEndBal = dblAmtandInt + dblPayment lblItemRows(0, pintRowCurrent).Text = (pintRowCurrent).ToString lblItemRows(1, pintRowCurrent).Text = dblInterest.ToString("c") lblItemRows(2, pintRowCurrent).Text = dblAmtandInt.ToString("c") lblItemRows(3, pintRowCurrent).Text = dblPayment.ToString("c") lblItemRows(4, pintRowCurrent).Text = dblEndBal.ToString("c") Next End Sub Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click Me.Close() End Sub End Class