Home

Example Application: Simple Interest II

 

The date picker control is a derivative of the date/time picker control. Like the month calendar control, it allows you to select a date from a calendar. It makes it possible to reduce, even eliminate, the likelihood of mistakes emanating from selecting a date. In this application, we use two date picker controls to let the user select a range of dates. We use that range to calculate the number of days that have elapsed and can be used to evaluate the age (number of days) of a loan.

     

Practical LearningPractical Learning: Creating the Application

  1. Start a new Window Application named SimpleInterest2
  2. In the Solution Explorer, right-click Form1.vb and click Rename
  3. Type SimpleInterest.vb and press Enter
  4. Design the form as follows:
     
    Simple Interest
    Control Text Name TextAlign
    GroupBox GroupBox Loan Preparation    
    Label Label Principal    
    TextBox TextBox 0.00 txtPrincipal Right
    Label Label Interest Rate:    
    TextBox TextBox 0.00 txtInterestRate Right
    Label Label %    
    Label Label Loan Start Date:    
    DateTimePicker DateTimePicker   dtpStartDate  
    Label Label Loan End Date:    
    DateTimePicker DateTimePicker   dtpLoandEndDate  
    Label Label Periods:    
    TextBox TextBox 1 txtPeriods Right
    Label Label days    
    Button Button Calculate btnCalculate  
    GroupBox GroupBox Results    
    Label Label Interest Earned:    
    TextBox TextBox 0.00 txtInterestEarned Right
    Label Label Future Value:    
    TextBox TextBox 0.00 txtFutureValue Right
    Button Button Close btnClose  
  5. Right-click the form and click View Code
  6. In the form Class Name combo box, select dtpEndDate
  7. In the Method Name combo box, select CloseUp and implement the event as follows:
     
    Private Sub dtpEndDateCloseUp(ByVal sender As Object, 
                                       ByVal e As System.EventArgs) 
                                       Handles dtpEndDate.CloseUp
            Dim Principal As Double
            Dim InterestRate As Double
            Dim Periods As Double
            Dim InterestEarned As Double
            Dim FutureValue
            Dim StartDate As DateTime
            Dim EndDate As DateTime
            Dim spnPeriods As TimeSpan
    
            ' Get the value of the principal
            Try
                Principal = CDbl(txtPrincipal.Text)
            Catch
                MsgBox("Invalid Principal Value")
            End Try
    
            ' Get the interest rate
            Try
                InterestRate = CDbl(txtInterestRate.Text) / 100
            Catch
                MsgBox("Invalid Interest Rate")
            End Try
    
            ' Get the start and end dates
            StartDate = dtpStartDate.Value
            EndDate = dtpEndDate.Value
    
            ' Make sure the end date doesn't occur before the start date
            If EndDate < StartDate Then
                MsgBox("Invalid Date Sequence: " & 
                                "the end date must occur after the start date")
                dtpEndDate.Value = DateTime.Today
                Return
            End If
    
            ' Get the difference in days
            ' The will be the periods
            spnPeriods = EndDate.Subtract(StartDate)
            Dim days As Integer = spnPeriods.Days
            txtPeriods.Text = CStr(days)
    
            Dim p As Double
    
            ' Because we will allow the user to directly specify 
            ' the number of days, let's get the period from its text box
            Try
                p = CDbl(txtPeriods.Text)
            Catch ex As Exception
                MsgBox("Invalid number of days")
            End Try
    
            ' The actual period is gotten as follows
            Periods = p / 365
            ' Now we can perform the calculations
            InterestEarned = Principal * InterestRate * Periods
            FutureValue = Principal + InterestEarned
    
            ' Display the values
            txtInterestEarned.Text = FormatCurrency(InterestEarned)
            txtFutureValue.Text = FormatCurrency(FutureValue)
    End Sub 
  8. In the form Class Name combo box, select txtPeriods
  9. In the Method Name combo box, select Leave and implement the event as follows:
     
    Private Sub txtPeriodsLeave(ByVal sender As Object, 
                                     ByVal e As System.EventArgs) 
                                     Handles txtPeriods.Leave
            ' If the user directly enters the number of days
            ' in the Periods text box and press Tab,
            ' we can perform the caltulation
            dtpEndDateCloseUp(sender, e)
    End Sub 
  10. In the form Class Name combo box, select btnCalculate
  11. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub btnCalculateClick(ByVal sender As Object, 
                                       ByVal e As System.EventArgs) 
                                       Handles btnCalculate.Click
            ' If the user directly enters the number of days
            ' in the Periods text box and clicks the Calculate
            ' button, we can perform the caltulation
            dtpEndDateCloseUp(sender, e)
    End Sub
  12. In the Class Name combo box, select btnClose
  13. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub btnCloseClick(ByVal sender As Object, 
                                   ByVal e As System.EventArgs) 
                                   Handles btnClose.Click
            End
    End Sub
  14. Return to the form
  15. In the Properties window, click the Properties button Properties
  16. On the form, click the top date picker control
  17. In the Properties window, change the following values:
    CalendarForeColor: Blue
    CalendarMonthBackground: SkyBlue
    CalendarTitleBackColor: Navy
    CalendarTitleForeColor: Gold
    CalendarTrailingForeColor: DodgeBlue
  18. On the form, click the top date picker control
  19. In the Properties window, change the following values:
    CalendarForeColor: Maroon
    CalendarMonthBackground: Orange
    CalendarTitleBackColor: Sienna
    CalendarTitleForeColor: Khaki
    CalendarTrailingForeColor: White
  20. Execute the application and test the application
     
  21. Close the form and return to your programming environment

Download 

 

Home Copyright © 2008-2016, FunctionX, Inc.