[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 176
  • Last Modified:

Display data values in a list

Could somebody please post some sample code (not an entire application) for how I can display a list of values, as depicted in this picture?

http://www.stevetout.com/list.gif
0
gmahler5th
Asked:
gmahler5th
  • 4
  • 3
  • 2
  • +1
2 Solutions
 
gmahler5thAuthor Commented:
Oh, by the way, the data in the list is based on the loan balance and interest paid for each payment over the term of the loan.
0
 
flavoCommented:
hey again!

this site here : http://support.microsoft.com/default.aspx?scid=kb;en-us;Q316303 shows how to do it basically

popultae an array and bind the array to a datagrid.

I dont have Visual Studio on me, but ill be home in about 1 1/2 hours to help if your still stuck!

Dave
0
 
arif_eqbalCommented:
I hope you can buld the upper half shown in the image
You need to put some textBoxes etc.

the lower part is a DataGrid, drag and drop a DataGrid on your form, and name it DetailsDataGrid, Then you would probably like to display details in it on the Click of the button "Display Details" as shown in the gif.
So put this declaration at Form Level

Dim DTDetails as dataTable

Now modify and put this code on the Button Click

        DTDetails = New DataTable
'Change your table name here
        Dim DA As OleDbDataAdapter = New OleDbDataAdapter("Select * From Mytable", ConStr)
        DA.Fill(DTDetails)

DetailsDataGrid.DataSource = DTDetails


0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
flavoCommented:
Got home about 15 mins ago, need to go out (getting hastled by the missus)

Its nearly there, except it puts all 0's/.. not 100% sure why, but im really sorry i cant finish it for you.

If you are still having problem, im sure someone will spot a simple stupid mistake of mine. Sorry..

Create a new class with this

Public Class repayment
    Private lmonth As Long
    Private dBalance As Double
    Private dInstall As Double
    Private dInterest As Double
    Private dPrincipal As Double

    Public Sub New(ByVal lMonth As Long, ByVal dBalance As Double, ByVal dInstall As Double, ByVal dInterest As Double, ByVal dPrincipal As Double)
        lMonth = Month
        dBalance = Balance
        dInstall = Installment
        dInterest = Interest
        dPrincipal = Principal
    End Sub

    Public Property Month() As Long
        Get
            Return lmonth
        End Get
        Set(ByVal Value As Long)
            lmonth = Value
        End Set
    End Property

    Public Property Balance() As Double
        Get
            Return dBalance
        End Get
        Set(ByVal Value As Double)
            dBalance = Value
        End Set
    End Property

    Public Property Installment() As Double
        Get
            Return dInstall
        End Get
        Set(ByVal Value As Double)
            dInstall = Value
        End Set
    End Property

    Public Property Interest() As Double
        Get
            Return dInterest
        End Get
        Set(ByVal Value As Double)
            dInterest = Value
        End Set
    End Property

    Public Property Principal() As Double
        Get
            Return dPrincipal
        End Get
        Set(ByVal Value As Double)
            dPrincipal = Value
        End Set
    End Property
End Class

'***************************************************************
'***************************************************************
'***************************************************************
'***************************************************************

then in the main form use this

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'values from your form
        Dim homeValue As Double = 250000
        Dim downPayment As Double = 50000
        Dim interest As Double = 7
        Dim loanDuration = 30
        Dim monthlyInstallment As Double = 1330.605

        'the good stuff
        Dim al As New ArrayList

        Dim i As Long
        Dim balanceLeft As Double = homeValue - downPayment
        Dim interestPaid As Double
        For i = 1 To loanDuration * 12
            interestPaid = balanceLeft * interest / 100 / 12
            balanceLeft = balanceLeft + interestPaid - monthlyInstallment
            al.Add(New repayment(i, balanceLeft, monthlyInstallment, interestPaid, interestPaid - monthlyInstallment))
        Next

        Me.DataGrid1.DataSource = al
    End Sub


'************************


Dave :-)
0
 
gmahler5thAuthor Commented:
Planocz, can you copy and paste the code that is relevant to just creating the list?  Your code is way to long for me to be able to identify what I am looking for.

The variables I am using are:

    'declare your variables here
    Dim PVal, APR, FVal, Payment, TotPmts, dblMonth, dblRateMonth As Double
    Dim Fmt As String

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

        'assign the values from textboxes to the variables
        PVal = txtPrincipal.Text
        APR = txtRate.Text
        If APR > 1 Then APR = APR / 100 ' Ensure proper form.
        TotPmts = txtYears.Text
        dblMonth = TotPmts * 12
        dblRateMonth = APR / 12
0
 
planoczCommented:
I just thru this together for a quick demo...

the numbers(answers) are not right, but you just wanted to see how to use the Listview box.

   Private Sub InitializeListview()
        Cursor.Current = Cursors.WaitCursor
        With ListView1
            .Columns.Add("Month", 50, HorizontalAlignment.Center)
            .Columns.Add("Balance", 70, HorizontalAlignment.Right)
            .Columns.Add("Installment", 80, HorizontalAlignment.Right)
            .Columns.Add("Interest Paid", 80, HorizontalAlignment.Right)
            .Columns.Add("Principal Paid", 80, HorizontalAlignment.Right)
            .View = View.Details
        End With
        Cursor.Current = Cursors.Default

        'These are just default values that you can start with
        txtPrincipal.Text = "250,000"       'Cost of Item
        txtRate.Text = "7"                      'Interest Rate
        txtYears.Text = "30"                   'Years to pay

    End Sub
    'declare your variables here
    Dim PVal, APR, FVal, Payment, TotPmts, dblMonth, dblRateMonth, Interest As Double

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
        Dim iCount As Short
        Dim iCounter As Short = 0
        Dim Fmt As String

        Fmt = "###,###,##0.00"          ' Define money format.

        'assign the values from textboxes to the variables
        PVal = txtPrincipal.Text
        APR = txtRate.Text
        If APR > 1 Then APR = APR / 100         ' Ensure proper form.
        TotPmts = txtYears.Text * 12
        dblMonth = TotPmts * 12
        dblRateMonth = APR / 12

        Interest = Payment - PVal
        Interest = (Int((Interest + 0.005) * 100) / 100)           ' Round interest.

        For iCount = 1 To TotPmts                                 'Number of months to pay loan
            With ListView1
                .Items.Add(UCase(iCount))                                           'Month
                .Items(iCounter).SubItems.Add(Payment)                      'Balance
                .Items(iCounter).SubItems.Add(Payment)                      'Payment (Installment)
                .Items(iCounter).SubItems.Add(Format(Interest, Fmt))   'Interest      
                .Items(iCounter).SubItems.Add(Format(PVal, Fmt))        'Principal
            End With
            iCounter += 1             'Column counter
        Next
    End Sub
0
 
gmahler5thAuthor Commented:
I tried using this code, but I don't see where the list should appear?  How do I change my form to accomodate displaying the list?
0
 
planoczCommented:
Your new code....

Public Class Form1
    Inherits System.Windows.Forms.Form

#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

    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 TabControl1 As System.Windows.Forms.TabControl
    Friend WithEvents TabPage1 As System.Windows.Forms.TabPage
    Friend WithEvents TabPage2 As System.Windows.Forms.TabPage
    Friend WithEvents txtPrincipal As System.Windows.Forms.TextBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents GroupBox4 As System.Windows.Forms.GroupBox
    Friend WithEvents RadioButton4 As System.Windows.Forms.RadioButton
    Friend WithEvents RadioButton5 As System.Windows.Forms.RadioButton
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents txtYears As System.Windows.Forms.TextBox
    Friend WithEvents txtRate As System.Windows.Forms.TextBox
    Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
    Friend WithEvents btnCalculate As System.Windows.Forms.Button
    Friend WithEvents txtAmount As System.Windows.Forms.TextBox
    Friend WithEvents GroupBox3 As System.Windows.Forms.GroupBox
    Friend WithEvents RadioButton3 As System.Windows.Forms.RadioButton
    Friend WithEvents RadioButton2 As System.Windows.Forms.RadioButton
    Friend WithEvents RadioButton1 As System.Windows.Forms.RadioButton
    Friend WithEvents ListView1 As System.Windows.Forms.ListView
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.TabControl1 = New System.Windows.Forms.TabControl
        Me.TabPage1 = New System.Windows.Forms.TabPage
        Me.GroupBox3 = New System.Windows.Forms.GroupBox
        Me.RadioButton3 = New System.Windows.Forms.RadioButton
        Me.RadioButton2 = New System.Windows.Forms.RadioButton
        Me.RadioButton1 = New System.Windows.Forms.RadioButton
        Me.GroupBox2 = New System.Windows.Forms.GroupBox
        Me.btnCalculate = New System.Windows.Forms.Button
        Me.txtAmount = New System.Windows.Forms.TextBox
        Me.GroupBox1 = New System.Windows.Forms.GroupBox
        Me.Label3 = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.txtYears = New System.Windows.Forms.TextBox
        Me.txtRate = New System.Windows.Forms.TextBox
        Me.GroupBox4 = New System.Windows.Forms.GroupBox
        Me.RadioButton4 = New System.Windows.Forms.RadioButton
        Me.RadioButton5 = New System.Windows.Forms.RadioButton
        Me.txtPrincipal = New System.Windows.Forms.TextBox
        Me.Label1 = New System.Windows.Forms.Label
        Me.TabPage2 = New System.Windows.Forms.TabPage
        Me.ListView1 = New System.Windows.Forms.ListView
        Me.TabControl1.SuspendLayout()
        Me.TabPage1.SuspendLayout()
        Me.GroupBox3.SuspendLayout()
        Me.GroupBox2.SuspendLayout()
        Me.GroupBox1.SuspendLayout()
        Me.GroupBox4.SuspendLayout()
        Me.TabPage2.SuspendLayout()
        Me.SuspendLayout()
        '
        'TabControl1
        '
        Me.TabControl1.Appearance = System.Windows.Forms.TabAppearance.FlatButtons
        Me.TabControl1.Controls.Add(Me.TabPage1)
        Me.TabControl1.Controls.Add(Me.TabPage2)
        Me.TabControl1.Location = New System.Drawing.Point(8, 4)
        Me.TabControl1.Name = "TabControl1"
        Me.TabControl1.SelectedIndex = 0
        Me.TabControl1.Size = New System.Drawing.Size(352, 328)
        Me.TabControl1.TabIndex = 16
        '
        'TabPage1
        '
        Me.TabPage1.Controls.Add(Me.GroupBox3)
        Me.TabPage1.Controls.Add(Me.GroupBox2)
        Me.TabPage1.Controls.Add(Me.GroupBox1)
        Me.TabPage1.Controls.Add(Me.GroupBox4)
        Me.TabPage1.Controls.Add(Me.txtPrincipal)
        Me.TabPage1.Controls.Add(Me.Label1)
        Me.TabPage1.Location = New System.Drawing.Point(4, 25)
        Me.TabPage1.Name = "TabPage1"
        Me.TabPage1.Size = New System.Drawing.Size(344, 299)
        Me.TabPage1.TabIndex = 0
        Me.TabPage1.Text = "Main Calculations"
        '
        'GroupBox3
        '
        Me.GroupBox3.Controls.Add(Me.RadioButton3)
        Me.GroupBox3.Controls.Add(Me.RadioButton2)
        Me.GroupBox3.Controls.Add(Me.RadioButton1)
        Me.GroupBox3.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.GroupBox3.Location = New System.Drawing.Point(172, 152)
        Me.GroupBox3.Name = "GroupBox3"
        Me.GroupBox3.Size = New System.Drawing.Size(144, 88)
        Me.GroupBox3.TabIndex = 19
        Me.GroupBox3.TabStop = False
        Me.GroupBox3.Text = "Predefined Rates"
        '
        'RadioButton3
        '
        Me.RadioButton3.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.RadioButton3.Location = New System.Drawing.Point(16, 60)
        Me.RadioButton3.Name = "RadioButton3"
        Me.RadioButton3.Size = New System.Drawing.Size(112, 24)
        Me.RadioButton3.TabIndex = 2
        Me.RadioButton3.Text = "30 Years  5.75%"
        '
        'RadioButton2
        '
        Me.RadioButton2.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.RadioButton2.Location = New System.Drawing.Point(16, 38)
        Me.RadioButton2.Name = "RadioButton2"
        Me.RadioButton2.TabIndex = 1
        Me.RadioButton2.Text = "15 Years  5.5%"
        '
        'RadioButton1
        '
        Me.RadioButton1.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.RadioButton1.Location = New System.Drawing.Point(16, 16)
        Me.RadioButton1.Name = "RadioButton1"
        Me.RadioButton1.Size = New System.Drawing.Size(112, 24)
        Me.RadioButton1.TabIndex = 0
        Me.RadioButton1.Text = "7 Years   5.35%"
        '
        'GroupBox2
        '
        Me.GroupBox2.Controls.Add(Me.btnCalculate)
        Me.GroupBox2.Controls.Add(Me.txtAmount)
        Me.GroupBox2.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.GroupBox2.Location = New System.Drawing.Point(180, 12)
        Me.GroupBox2.Name = "GroupBox2"
        Me.GroupBox2.Size = New System.Drawing.Size(120, 120)
        Me.GroupBox2.TabIndex = 18
        Me.GroupBox2.TabStop = False
        Me.GroupBox2.Text = "Payment Amount"
        '
        'btnCalculate
        '
        Me.btnCalculate.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.btnCalculate.ForeColor = System.Drawing.SystemColors.HotTrack
        Me.btnCalculate.Location = New System.Drawing.Point(24, 80)
        Me.btnCalculate.Name = "btnCalculate"
        Me.btnCalculate.Size = New System.Drawing.Size(72, 23)
        Me.btnCalculate.TabIndex = 4
        Me.btnCalculate.Text = "Calculate"
        '
        'txtAmount
        '
        Me.txtAmount.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.txtAmount.ForeColor = System.Drawing.SystemColors.ControlDarkDark
        Me.txtAmount.Location = New System.Drawing.Point(10, 32)
        Me.txtAmount.Name = "txtAmount"
        Me.txtAmount.TabIndex = 8
        Me.txtAmount.Text = ""
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.Add(Me.Label3)
        Me.GroupBox1.Controls.Add(Me.Label2)
        Me.GroupBox1.Controls.Add(Me.txtYears)
        Me.GroupBox1.Controls.Add(Me.txtRate)
        Me.GroupBox1.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.GroupBox1.Location = New System.Drawing.Point(8, 144)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(136, 148)
        Me.GroupBox1.TabIndex = 17
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "My Rates"
        '
        'Label3
        '
        Me.Label3.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label3.Location = New System.Drawing.Point(12, 88)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(100, 16)
        Me.Label3.TabIndex = 10
        Me.Label3.Text = "Number of years"
        '
        'Label2
        '
        Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label2.Location = New System.Drawing.Point(12, 32)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(88, 12)
        Me.Label2.TabIndex = 9
        Me.Label2.Text = "Interest Rate"
        '
        'txtYears
        '
        Me.txtYears.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.txtYears.ForeColor = System.Drawing.SystemColors.ControlDarkDark
        Me.txtYears.Location = New System.Drawing.Point(16, 112)
        Me.txtYears.Name = "txtYears"
        Me.txtYears.TabIndex = 3
        Me.txtYears.Text = ""
        '
        'txtRate
        '
        Me.txtRate.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.txtRate.ForeColor = System.Drawing.SystemColors.ControlDarkDark
        Me.txtRate.Location = New System.Drawing.Point(16, 56)
        Me.txtRate.Name = "txtRate"
        Me.txtRate.TabIndex = 2
        Me.txtRate.Text = ""
        '
        'GroupBox4
        '
        Me.GroupBox4.Controls.Add(Me.RadioButton4)
        Me.GroupBox4.Controls.Add(Me.RadioButton5)
        Me.GroupBox4.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.GroupBox4.Location = New System.Drawing.Point(8, 56)
        Me.GroupBox4.Name = "GroupBox4"
        Me.GroupBox4.Size = New System.Drawing.Size(155, 80)
        Me.GroupBox4.TabIndex = 16
        Me.GroupBox4.TabStop = False
        Me.GroupBox4.Text = "Calculation Type"
        '
        'RadioButton4
        '
        Me.RadioButton4.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.RadioButton4.Location = New System.Drawing.Point(8, 48)
        Me.RadioButton4.Name = "RadioButton4"
        Me.RadioButton4.Size = New System.Drawing.Size(144, 24)
        Me.RadioButton4.TabIndex = 13
        Me.RadioButton4.Text = "Select Predefined Rate"
        '
        'RadioButton5
        '
        Me.RadioButton5.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.RadioButton5.Location = New System.Drawing.Point(8, 21)
        Me.RadioButton5.Name = "RadioButton5"
        Me.RadioButton5.Size = New System.Drawing.Size(136, 24)
        Me.RadioButton5.TabIndex = 14
        Me.RadioButton5.Text = "Choose my own rates"
        '
        'txtPrincipal
        '
        Me.txtPrincipal.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.txtPrincipal.ForeColor = System.Drawing.SystemColors.ControlDarkDark
        Me.txtPrincipal.Location = New System.Drawing.Point(12, 28)
        Me.txtPrincipal.Name = "txtPrincipal"
        Me.txtPrincipal.Size = New System.Drawing.Size(125, 20)
        Me.txtPrincipal.TabIndex = 9
        Me.txtPrincipal.Text = ""
        '
        'Label1
        '
        Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label1.Location = New System.Drawing.Point(8, 8)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(96, 16)
        Me.Label1.TabIndex = 10
        Me.Label1.Text = "Principal"
        '
        'TabPage2
        '
        Me.TabPage2.Controls.Add(Me.ListView1)
        Me.TabPage2.Location = New System.Drawing.Point(4, 25)
        Me.TabPage2.Name = "TabPage2"
        Me.TabPage2.Size = New System.Drawing.Size(344, 299)
        Me.TabPage2.TabIndex = 1
        Me.TabPage2.Text = "View Results"
        '
        'ListView1
        '
        Me.ListView1.Location = New System.Drawing.Point(8, 8)
        Me.ListView1.Name = "ListView1"
        Me.ListView1.Size = New System.Drawing.Size(328, 284)
        Me.ListView1.TabIndex = 0
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(372, 341)
        Me.Controls.Add(Me.TabControl1)
        Me.Name = "Form1"
        Me.Text = "Mortgage Calculator"
        Me.TabControl1.ResumeLayout(False)
        Me.TabPage1.ResumeLayout(False)
        Me.GroupBox3.ResumeLayout(False)
        Me.GroupBox2.ResumeLayout(False)
        Me.GroupBox1.ResumeLayout(False)
        Me.GroupBox4.ResumeLayout(False)
        Me.TabPage2.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    'declare your variables here
    Dim PVal, APR, FVal, Payment, TotPmts, dblMonth, dblRateMonth, Interest As Double
    Dim Fmt As String

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

        Dim iCount As Short
        Dim iCounter As Short = 0

        InitializeListview()

        'assign the values from textboxes to the variables
        PVal = txtPrincipal.Text
        APR = txtRate.Text
        If APR > 1 Then APR = APR / 100 ' Ensure proper form.
        TotPmts = txtYears.Text
        dblMonth = TotPmts * 12
        dblRateMonth = APR / 12


        'calculate the final amount as principal + interest
        Fmt = "###,###,##0.00"   ' Define money format.
        FVal = 0   ' Usually 0 for a loan.

        Payment = PVal / ((1 - (1 + dblRateMonth) ^ -dblMonth) / dblRateMonth)

        'Payment = Pmt(APR / 12, TotPmts, -PVal, FVal) Tried using the builtin payment function.  Couldn't get it to work.

        'put the calculated amount in Payment Amount textbox
        txtAmount.Text = Format(Payment, Fmt)
        Interest = Payment - PVal
        Interest = (Int((Interest + 0.005) * 100) / 100)           ' Round interest.

        For iCount = 1 To TotPmts                                 'Number of months to pay loan
            With ListView1
                .Items.Add(UCase(iCount))                               'Month
                .Items(iCounter).SubItems.Add(Payment)                  'Balance
                .Items(iCounter).SubItems.Add(Payment)                  'Payment (Installment)
                .Items(iCounter).SubItems.Add(Format(Interest, Fmt))    'Interest      
                .Items(iCounter).SubItems.Add(Format(PVal, Fmt))        'Principal
            End With
            iCounter += 1             'Column counter
        Next

    End Sub
    Private Sub InitializeListview()
        Cursor.Current = Cursors.WaitCursor
        With ListView1
            .Columns.Add("Month", 50, HorizontalAlignment.Center)
            .Columns.Add("Balance", 70, HorizontalAlignment.Right)
            .Columns.Add("Installment", 80, HorizontalAlignment.Right)
            .Columns.Add("Interest Paid", 80, HorizontalAlignment.Right)
            .Columns.Add("Principal Paid", 80, HorizontalAlignment.Right)
            .View = View.Details
        End With
        Cursor.Current = Cursors.Default

        'These are just default values that you can start with
        txtPrincipal.Text = "250,000"       'Cost of Item
        txtRate.Text = "7"                      'Interest Rate
        txtYears.Text = "30"                   'Years to pay

    End Sub
End Class


0
 
planoczCommented:
Change this in the above code.....

    'declare your variables here
    Dim PVal, APR, FVal, P, Payment, TotPmts, dblMonth, dblRateMonth, Interest As Double
    Dim Balance, Period As Double
    Dim Fmt As String
    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

        Dim iCount As Short
        Dim iCounter As Short = 0

        Cursor.Current = Cursors.WaitCursor

        InitializeListview()

        Fmt = "###,###,##0.00"   ' Define money format.

        FVal = 0   ' Usually 0 for a loan.
        PVal = CDbl(Trim(txtPrincipal.Text))
        APR = CDbl(Trim(txtRate.Text))
        If APR > 1 Then APR = APR / 100 ' Ensure proper form.
        TotPmts = CDbl(Trim(txtYears.Text * 12))

        Payment = Math.Abs(-Pmt(APR / 12, TotPmts, PVal, FVal, 0))
        'put the calculated amount in Payment Amount textbox
        txtAmount.Text = Format(Payment, Fmt)
        Period = 1
        Balance = PVal
        For iCount = 1 To TotPmts
            P = PPmt(APR / 12, Period, TotPmts, -PVal, FVal, 0)
            P = (Int((P + 0.005) * 100) / 100)  ' Round principal.
            Interest = Payment - P
            Interest = (Int((Interest + 0.005) * 100) / 100)  ' Round interest.
            Balance = Balance - (Payment - Interest)
            With ListView1
                .Items.Add(UCase(iCount))                                            'Month
                .Items(iCounter).SubItems.Add(Format(Balance, Fmt))     'Balance  
                .Items(iCounter).SubItems.Add(Format(Payment, Fmt))     'Installment
                .Items(iCounter).SubItems.Add(Format(Interest, Fmt))      'Interest      
                .Items(iCounter).SubItems.Add(Format(P, Fmt))                'Principal
            End With
            iCounter += 1
            Period += 1
        Next
        Cursor.Current = Cursors.Default
    End Sub
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now