Practical Learning: Using a Picture on a Button
|
|
- To start a new application, on the main menu, click File -> New ->
Project
- In the Templates list, click Windows Application and change the Name to
SimpleInterest1
- Click OK
- On the main menu, click Project -> Add New Item...
- In the Templates list, click Icon File
- Change the Name to Calculator and click OK
- Right-click a white area in the icon designer -> Delete Image Type
- Right-click the icon designer again -> Current Icon Image Types ->
16x16, 16 colors
- Design the icon as follows:
- On the main menu, click Project -> Add New Item...
- In the Templates list, click Icon File and change the Name to exit
- Click Add
- Right-click a white area in the icon designer -> Delete Image Type
- Right-click the icon designer again -> Current Icon Image Types ->
16x16, 16 colors
- Design the icon as follows:
- Save and close the icon window
- In the Solution Explorer, right-click Form1.cs and click Rename
- Type SimpleInterest.vb and press Enter twice
- 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 |
Periods: |
|
|
TextBox |
1 |
txtPeriods |
Right |
Label |
Months |
|
|
Button |
Calculate |
btnCalculate |
|
Button |
Close |
btnClose |
|
GroupBox |
Results |
|
|
Label |
Interest Earned: |
|
|
TextBox |
0.00 |
txtInterestEarned |
Right |
Label |
Future Value |
|
|
TextBox |
0.00 |
txtFutureValue |
Right |
|
- On the form, click the Calculate button
- In the Properties window, click Image and click its ellipsis button
- In the Select Resource dialog box, click Import
- In the Files of Type box, select All Files
- Locate the folder of the current project, select Calculator.ico and
click Open
- On the Select Resource dialog box, click OK
- Change the following properties of the Calculate button:
ImageAlign: TopCenter
TextAlign: BottomCenter
- On the form, click the Calculate button
- In the Properties window, click Image and click its ellipsis button
- In the Select Resource dialog box, click Import
- In the Files of Type box, select All Files
- Locate the folder of the current project, select exit.ico and click Open
- On the Select Resource dialog box, click OK
- Change the following properties of the Close button:
ImageAlign: TopCenter
TextAlign: BottomCenter
- 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 Periods As Double
Dim I As Double
Dim p As Double
Try
Principal = CDbl(txtPrincipal.Text)
Catch ex As Exception
MsgBox("The value you entered for the " &
"principal is not valid")
End Try
Try
InterestRate = CDbl(txtInterestRate.Text)
Catch ex As Exception
MsgBox("Wrong Value: The interest rate must " &
"be a value between 0 and 100")
End Try
Try
Periods = CDbl(txtPeriods.Text)
Catch ex As Exception
MsgBox("You entered an invalid value for the periods")
End Try
i = InterestRate / 100
p = Periods / 12
InterestEarned = Principal * i * p
FutureValue = Principal + InterestEarned
txtInterestEarned.Text = InterestEarned.ToString("C")
txtFutureValue.Text = FutureValue.ToString("C")
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
|
- Execute the application and test the calculation
- Close the form and return to your programming environment
|
|