Practical Learning: Introducing Radio Buttons
|
|
The application we are going to create is used to calculate
the amount owed on a loan using the following formula:
|
P = Principal |
r = Annual (Interest) Rate |
m = Number of Compounding Periods per Year |
n = Total Number of Compounding Periods |
A = Amount Earned After n periods |
- Start a new Windows Application named CompoundInterest1
- In the Solution Explorer, right-click Form1.vb and click Rename
- Type CompoundInterest.vb and press Enter
- Design the form as followed:
|
Control |
Name |
Text |
Additional Properties |
GroupBox |
|
|
Loan Setup |
|
Label |
|
|
Principal: |
|
TextBox |
|
txtPrincipal |
0.00 |
TextAlign: Right |
Label |
|
|
Interest Rate: |
|
TextBox |
|
txtInterestRate |
8.25 |
TextAlign: Right |
Label |
|
|
% |
|
Label |
|
|
Number of Periods: |
|
TextBox |
|
txtPeriods |
1 |
TextAlign: Text |
Label |
|
|
years |
|
GroupBox |
|
|
Compound Frequency |
|
RadioButton |
|
rdoMonthly |
|
|
RadioButton |
|
rdoQuarterly |
|
|
RadioButton |
|
rdoSemiannually |
|
|
RadioButton |
|
rdoAnnually |
|
|
GroupBox |
|
|
Results |
|
Label |
|
|
Interest Earned: |
|
TextBox |
|
txtInterestEarned |
0.00 |
TextAlign: Right
ReadOnly: True |
Label |
|
|
Amount Earned: |
|
TextBox |
|
txtFutureValue |
0.00 |
TextAlign: Right
ReadOnly: True |
Button |
|
btnCalculate |
Calculate |
|
Button |
|
btnClose |
Close |
|
|
- On the form, double-click the Close button and implement the event as
follows:
Private Sub btnCloseClick(ByVal sender As Object,
ByVal e As System.EventArgs)
Handles btnClose.Click
End
End Sub
|
- On the form, click Monthly
- In the Properties window, double-click Checked to set its value to True
- On the form, double-click the Calculate button and implement its Click()
event as follows:
Private Sub btnCalculateClick(ByVal sender As System.Object,
ByVal e As System.EventArgs)
Handles btnCalculate.Click
Dim Principal As Double
Dim InterestRate As Double
Dim InterestEarned As Double
Dim FutureValue As Double
Dim RatePerPeriod As Double
Dim Periods As Double
Dim CompoundType As Integer
' Retrieve the value of the principal
Try
Principal = CDbl(txtPrincipal.Text)
Catch ex As Exception
MsgBox("The value you entered for the principal " &
"is not valid" & vbCrLf & "Please try again")
End Try
' Retrieve the interest rate
Try
InterestRate = CDbl(txtInterestRate.Text) / 100
Catch ex As Exception
MsgBox("The value you entered for the interest rate " &
"is not valid" & vbCrLf & "Please try again")
End Try
' Find out what radio button was clicked to apply the compound frequency
If rdoMonthly.Checked Then
CompoundType = 12
ElseIf rdoQuarterly.Checked Then
CompoundType = 4
ElseIf rdoSemiannually.Checked Then
CompoundType = 2
Else
CompoundType = 1
End If
' Get the number of periods
Try
Periods = CDbl(txtPeriods.Text)
Catch ex As Exception
MsgBox("The value you entered for the number of periods " &
"is not valid" & vbCrLf & "Please try again")
End Try
' These values will make the calculation easier to read
Dim i As Double = InterestRate / CompoundType
Dim n As Double = CompoundType * Periods
' Perform the necessary calculations
RatePerPeriod = InterestRate / Periods
FutureValue = Principal * Math.Pow(1 + i, n)
interestEarned = FutureValue - Principal
' Display the values in the appropriate text boxes
txtInterestEarned.Text = FormatCurrency(InterestEarned)
txtFutureValue.Text = FormatCurrency(FutureValue)
End Sub
|
- Test the application
- After using it, close the form and return to your programming
environment
Compound Interest
|
|