Practical
Learning: Creating the Application
|
|
- Start a new Window Application named SimpleInterest2
- In the Solution Explorer, right-click Form1.vb and click Rename
- Type SimpleInterest.vb and press Enter
- Design the form as follows:
|
Control |
Text |
Name |
TextAlign |
GroupBox |
|
Loan Preparation |
|
|
Label |
|
Principal |
|
|
TextBox |
|
0.00 |
txtPrincipal |
Right |
Label |
|
Interest Rate: |
|
|
TextBox |
|
0.00 |
txtInterestRate |
Right |
Label |
|
% |
|
|
Label |
|
Loan Start Date: |
|
|
DateTimePicker |
|
|
dtpStartDate |
|
Label |
|
Loan End Date: |
|
|
DateTimePicker |
|
|
dtpLoandEndDate |
|
Label |
|
Periods: |
|
|
TextBox |
|
1 |
txtPeriods |
Right |
Label |
|
days |
|
|
Button |
|
Calculate |
btnCalculate |
|
GroupBox |
|
Results |
|
|
Label |
|
Interest Earned: |
|
|
TextBox |
|
0.00 |
txtInterestEarned |
Right |
Label |
|
Future Value: |
|
|
TextBox |
|
0.00 |
txtFutureValue |
Right |
Button |
|
Close |
btnClose |
|
|
- Right-click the form and click View Code
- In the form Class Name combo box, select dtpEndDate
- 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
|
- In the form Class Name combo box, select txtPeriods
- 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
|
- In the form Class Name combo box, select btnCalculate
- 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
|
- In the Class Name combo box, select btnClose
- 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
|
- Return to the form
- In the Properties window, click the Properties button
- On the form, click the top date picker control
- In the Properties window, change the following values:
CalendarForeColor: Blue
CalendarMonthBackground: SkyBlue
CalendarTitleBackColor: Navy
CalendarTitleForeColor: Gold
CalendarTrailingForeColor: DodgeBlue
- On the form, click the top date picker control
- In the Properties window, change the following values:
CalendarForeColor: Maroon
CalendarMonthBackground: Orange
CalendarTitleBackColor: Sienna
CalendarTitleForeColor: Khaki
CalendarTrailingForeColor: White
- Execute the application and test the application
- Close the form and return to your programming environment
Download |
|