Home

Example Application: Simple Interest

 

Interest Rate

The Caption of a Button

This application demonstrates some properties of the button control of Microsoft Windows. The two properties that are mostly used here are the text alignment and the image on a button.

To make it useful, this example calculates the future value of a loan using its present value (the principal), its interest rate, and its period (to make this application a little simpler, the period represents the number of months).

Windows Controls:

     

Practical Learning Practical Learning: Using a Picture on a Button

  1. To start a new application, on the main menu, click File -> New -> Project
  2. In the Templates list, click Windows Application and change the Name to SimpleInterest1
  3. Click OK
  4. On the main menu, click Project -> Add New Item...
  5. In the Templates list, click Icon File
  6. Change the Name to Calculator and click OK
  7. Right-click a white area in the icon designer -> Delete Image Type
  8. Right-click the icon designer again -> Current Icon Image Types -> 16x16, 16 colors
  9. Design the icon as follows:
     
    Calculator
  10. On the main menu, click Project -> Add New Item...
  11. In the Templates list, click Icon File and change the Name to exit
  12. Click Add
  13. Right-click a white area in the icon designer -> Delete Image Type
  14. Right-click the icon designer again -> Current Icon Image Types -> 16x16, 16 colors
  15. Design the icon as follows:
     
    Exit
  16. Save and close the icon window
  17. In the Solution Explorer, right-click Form1.cs and click Rename
  18. Type SimpleInterest.vb and press Enter twice
  19. Design the form as follows:
     
    Simple Interest
    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
  20. On the form, click the Calculate button
  21. In the Properties window, click Image and click its ellipsis button
  22. In the Select Resource dialog box, click Import
  23. In the Files of Type box, select All Files
  24. Locate the folder of the current project, select Calculator.ico and click Open
  25. On the Select Resource dialog box, click OK
  26. Change the following properties of the Calculate button:
    ImageAlign: TopCenter
    TextAlign: BottomCenter
  27. On the form, click the Calculate button
  28. In the Properties window, click Image and click its ellipsis button
  29. In the Select Resource dialog box, click Import
  30. In the Files of Type box, select All Files
  31. Locate the folder of the current project, select exit.ico and click Open
  32. On the Select Resource dialog box, click OK
  33. Change the following properties of the Close button:
    ImageAlign: TopCenter
    TextAlign: BottomCenter
     
    Simple Interest
  34. 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
  35. In the Class Name combo box, select btnClose
  36. 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
  37. Execute the application and test the calculation
     
    Interest Rate
  38. Close the form and return to your programming environment
 

Home Copyright © 2008-2016, FunctionX, Inc.