Option Explicit On 
Option Strict On

Public Class frmSavings
  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
  Friend WithEvents Label2 As System.Windows.Forms.Label
  Friend WithEvents Label3 As System.Windows.Forms.Label
  Friend WithEvents txtDeposit As System.Windows.Forms.TextBox
  Friend WithEvents txtInterest As System.Windows.Forms.TextBox
  Friend WithEvents txtMonths As System.Windows.Forms.TextBox
  Friend WithEvents txtFinal As System.Windows.Forms.TextBox
  Friend WithEvents Label4 As System.Windows.Forms.Label
  Friend WithEvents btnExit As System.Windows.Forms.Button
  Friend WithEvents Label1 As System.Windows.Forms.Label
  Friend WithEvents btnClear As System.Windows.Forms.Button
  Friend WithEvents btnCalculate As System.Windows.Forms.Button

  'Required by the Windows Form Designer
  Private components As System.ComponentModel.Container

  '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.
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Me.Label4 = New System.Windows.Forms.Label()
    Me.Label1 = New System.Windows.Forms.Label()
    Me.Label2 = New System.Windows.Forms.Label()
    Me.Label3 = New System.Windows.Forms.Label()
    Me.btnCalculate = New System.Windows.Forms.Button()
    Me.btnExit = New System.Windows.Forms.Button()
    Me.btnClear = New System.Windows.Forms.Button()
    Me.txtInterest = New System.Windows.Forms.TextBox()
    Me.txtMonths = New System.Windows.Forms.TextBox()
    Me.txtFinal = New System.Windows.Forms.TextBox()
    Me.txtDeposit = New System.Windows.Forms.TextBox()
    Me.SuspendLayout()
    '
    'Label4
    '
    Me.Label4.Location = New System.Drawing.Point(8, 112)
    Me.Label4.Name = "Label4"
    Me.Label4.TabIndex = 0
    Me.Label4.Text = "Final Balance"
    '
    'Label1
    '
    Me.Label1.Location = New System.Drawing.Point(8, 16)
    Me.Label1.Name = "Label1"
    Me.Label1.TabIndex = 0
    Me.Label1.Text = "Monthly Deposit"
    '
    'Label2
    '
    Me.Label2.Location = New System.Drawing.Point(8, 48)
    Me.Label2.Name = "Label2"
    Me.Label2.TabIndex = 0
    Me.Label2.Text = "Yearly Interest"
    '
    'Label3
    '
    Me.Label3.Location = New System.Drawing.Point(8, 80)
    Me.Label3.Name = "Label3"
    Me.Label3.TabIndex = 0
    Me.Label3.Text = "Number of Months"
    '
    'btnCalculate
    '
    Me.btnCalculate.Location = New System.Drawing.Point(24, 144)
    Me.btnCalculate.Name = "btnCalculate"
    Me.btnCalculate.Size = New System.Drawing.Size(80, 23)
    Me.btnCalculate.TabIndex = 4
    Me.btnCalculate.Text = "&Calculate"
    '
    'btnExit
    '
    Me.btnExit.Location = New System.Drawing.Point(80, 176)
    Me.btnExit.Name = "btnExit"
    Me.btnExit.TabIndex = 3
    Me.btnExit.TabStop = False
    Me.btnExit.Text = "E&xit"
    '
    'btnClear
    '
    Me.btnClear.Location = New System.Drawing.Point(128, 144)
    Me.btnClear.Name = "btnClear"
    Me.btnClear.Size = New System.Drawing.Size(80, 23)
    Me.btnClear.TabIndex = 5
    Me.btnClear.Text = "Clear &Boxes"
    '
    'txtInterest
    '
    Me.txtInterest.Location = New System.Drawing.Point(120, 48)
    Me.txtInterest.Name = "txtInterest"
    Me.txtInterest.TabIndex = 1
    Me.txtInterest.Text = ""
    '
    'txtMonths
    '
    Me.txtMonths.Location = New System.Drawing.Point(120, 80)
    Me.txtMonths.Name = "txtMonths"
    Me.txtMonths.TabIndex = 2
    Me.txtMonths.Text = ""
    '
    'txtFinal
    '
    Me.txtFinal.Location = New System.Drawing.Point(120, 112)
    Me.txtFinal.Name = "txtFinal"
    Me.txtFinal.TabIndex = 3
    Me.txtFinal.Text = ""
    '
    'txtDeposit
    '
    Me.txtDeposit.Location = New System.Drawing.Point(120, 16)
    Me.txtDeposit.Name = "txtDeposit"
    Me.txtDeposit.TabIndex = 0
    Me.txtDeposit.Text = ""
    '
    'frmSavings
    '
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(246, 219)
    Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnClear, Me.btnExit, Me.btnCalculate, Me.Label4, Me.txtFinal, Me.txtMonths, Me.txtInterest, Me.txtDeposit, Me.Label3, Me.Label2, Me.Label1})
    Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
    Me.Name = "frmSavings"
    Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
    Me.Text = "Savings Account"
    Me.ResumeLayout(False)

  End Sub

#End Region
  Dim Deposit As Double
  Dim Interest As Double
  Dim Months As Double
  Dim Final As Double

  Private Sub btncalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
    Dim IntRate As Double
    Dim FinalCompute As Double, IntChange As Double, IntDirection As Integer
    'Read the four text boxes
    Deposit = Val(txtDeposit.Text)
    Interest = Val(txtInterest.Text)
    IntRate = Interest / 1200
    Months = Val(txtMonths.Text)
    Final = Val(txtFinal.Text)
    'Determine which box is blank
    'Compute that missing value and put in text box
    If Trim(txtDeposit.Text) = "" Then
      'Deposit missing
      If Interest = 0 Then
        Deposit = Final / Months
      Else
        Deposit = Final / (((1 + IntRate) ^ Months - 1) / IntRate)
      End If
      txtDeposit.Text = Format(Deposit, "0.00")
    ElseIf Trim(txtInterest.Text) = "" Then
      'Interest missing - requires iterative solution
      'IntChange is how much we change interest each step
      'IntDirection is direction (+ or -) we change interest
      Interest = 0
      IntChange = 1
      IntDirection = 1
      Do
        Interest += IntDirection * IntChange
        IntRate = Interest / 1200
        FinalCompute = Deposit * ((1 + IntRate) ^ Months - 1) / IntRate
        If IntDirection = 1 Then
          If FinalCompute > Final Then
            IntDirection = -1
            IntChange /= 10
          End If
        Else
          If FinalCompute < Final Then
            IntDirection = 1
            IntChange /= 10
          End If
        End If
      Loop Until Math.Abs(FinalCompute - Final) < 0.005
      txtInterest.Text = Format(Interest, "0.00")
    ElseIf Trim(txtMonths.Text) = "" Then
      'Months missing
      If Interest = 0 Then
        Months = Final / Deposit
      Else
        Months = Math.Log(Final * IntRate / Deposit + 1) / Math.Log(1 + IntRate)
      End If
      txtMonths.Text = Format(Months, "0.00")
    ElseIf Trim(txtFinal.Text) = "" Then
      'Final value missing
      If Interest = 0 Then
        Final = Deposit * Months
      Else
        Final = Deposit * ((1 + IntRate) ^ Months - 1) / IntRate
      End If
      txtFinal.Text = Format(Final, "0.00")
    End If
  End Sub


  Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    Me.Close()
  End Sub





  Private Sub txtDeposit_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtDeposit.KeyPress
    'only allow numbers, a single decimal point, backspace  or enter        
    Select Case e.KeyChar
      Case CChar("0") To CChar("9"), ControlChars.Back
        'acceptable keystrokes 
        e.Handled = False
      Case ControlChars.Cr
        'enter key - move to next box
        txtInterest.Focus()
        e.Handled = False
      Case CChar(".")
        'check for existence of decimal point
        If InStr(txtDeposit.Text, ".") = 0 Then
          e.Handled = False
        Else
          e.Handled = True
        End If
      Case Else
        e.Handled = True
    End Select
  End Sub

  Private Sub txtInterest_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtInterest.KeyPress
   'only allow numbers, a single decimal point, backspace  or enter        
    Select Case e.KeyChar
      Case CChar("0") To CChar("9"), ControlChars.Back
        'acceptable keystrokes 
        e.Handled = False
      Case ControlChars.Cr
        'enter key - move to next box
        txtMonths.Focus()
        e.Handled = False
      Case CChar(".")
        'check for existence of decimal point
        If InStr(txtInterest.Text, ".") = 0 Then
          e.Handled = False
        Else
          e.Handled = True
        End If
      Case Else
        e.Handled = True
    End Select
  End Sub

  Private Sub txtMonths_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtMonths.KeyPress
    'only allow numbers,  backspace  or enter        
    Select Case e.KeyChar
      Case CChar("0") To CChar("9"), ControlChars.Back
        'acceptable keystrokes 
        e.Handled = False
      Case ControlChars.Cr
        'enter key - move to final box
        txtFinal.Focus()
        e.Handled = False
      Case Else
        e.Handled = True
    End Select
  End Sub

  Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
    'blank out text boxes
    txtDeposit.Text = ""
    txtInterest.Text = ""
    txtMonths.Text = ""
    txtFinal.Text = ""
    txtDeposit.Focus()
  End Sub

  Private Sub txtFinal_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtFinal.KeyPress
    'only allow numbers, a single decimal point, backspace  or enter        
    Select Case e.KeyChar
      Case CChar("0") To CChar("9"), ControlChars.Back
        'acceptable keystrokes 
        e.Handled = False
      Case ControlChars.Cr
        'enter key - move to calculate button
        btnCalculate.Focus()
        e.Handled = False
      Case CChar(".")
        'check for existence of decimal point
        If InStr(txtFinal.Text, ".") = 0 Then
          e.Handled = False
        Else
          e.Handled = True
        End If
      Case Else
        e.Handled = True
    End Select
  End Sub
End Class
