Practice Exercise 5
Looping

     '     Student Name
     '     Date
     '     Depreciation Table
     '     The purpose of this program is to calculate the depreciation
     '     of a asset over X number of years. This is a very valuable
     '     tool for accountants.
     '----------------------------------------------------------------
     Private Sub cmdCalculate_Click(ByVal sender As System.Object, _
                                                     ByVal e As System.EventArgs) _
                                                     Handles cmdCalculate.Click
          ' Declare variables
          Dim price As Long
          Dim term As Integer
          Dim depreciation As Long
          Dim accumDepreciation As Long
          Dim year As Integer

          '------------
          ' INPUT
          '------------
          price = txtAmount.Text
          term = txtYears.Text

          '------------
          ' Calculate depreciation amount
          '------------
          depreciation = price / term

          '------------
          ' Generate headings
          '------------
          lstOutput.Items.Clear() ' Clear listbox
          lstOutput.Items.Add(Space(26) & "EOY" & Space(6) & "Accum.")
          lstOutput.Items.Add(" Year" & Space(7) & "Depr." & Space(7) & "Value" & Space(7) & "Depr.")
          lstOutput.Items.Add(StrDup(40, "-"))

          '------------
          ' Processing
          '------------
          For year = 1 To term
               If (depreciation > price) Then
                    depreciation = price
              
End If
               price = price - depreciation
               accumDepreciation = accumDepreciation + depreciation
              
'------------
               ' Output
               '------------
               lstOutput.Items.Add(Space$(5 - numdigits(year)) _
                                         & year _
                                         & Space$(12 - FormatCurrency(depreciation, 0).Length) _
                                         & FormatCurrency(depreciation, 0) _
                                         & Space$(12 - FormatCurrency(price, 0).Length) _
                                         & FormatCurrency(price, 0) _
                                         & Space$(12 - FormatCurrency(accumDepreciation, 0).Length) _
                                         & FormatCurrency(accumDepreciation, 0))
          Next year
     End Sub

     '--------------------------------------------------
     ' This function determines the number
     ' of digits in a numeric value.
     '--------------------------------------------------
    
Private Function numdigits(ByVal num As Object) As Integer
          numdigits = num.ToString.Length
     End Function


     '--------------------------------------------------
     ' This sub handles the Exit button click event.
     '--------------------------------------------------
     Private Sub cmdExit_Click(ByVal sender As System.Object, _
                                             ByVal e As System.EventArgs) _
                                             Handles cmdExit.Click
         Me.Close()
     End Sub