Home

Example Application: Simple Interest

Description

This example explores the techniques of using the button control by adding bitmaps to them. The application is used to calculate the amount of interest accumulated on a loan.

Practical LearningPractical Learning: Using a Picture on a Button

  1. Start Microsoft Visual Studio
  2. To start a new application, on the main menu, click File -> New Project...
  3. In the middle list, click Windows Application and change the Name to SimpleInterest1
  4. Click OK
  5. On the main menu, click Project -> Add New Item...
  6. In the middle list, click Icon File
  7. Change the Name to Calculator and click OK
  8. Right-click a white area in the icon designer -> Delete Image Type
  9. Right-click the icon designer again -> Current Icon Image Types -> 16x16, 16 colors
  10. Design the icon as follows:
     
    Calculator
  11. On the main menu, click Project -> Add New Item...
  12. In the middle list, click Icon File and change the Name to exit
  13. Click Add
  14. Right-click a white area in the icon designer -> Delete Image Type
  15. Right-click the icon designer again -> Current Icon Image Types -> 16x16, 16 colors
  16. Design the icon as follows:
     
    Exit
  17. Save and close the icon window
  18. In the Solution Explorer, right-click Form1.cs and click Rename
  19. Type SimpleInterest.cs and press Enter twice
  20. 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
  21. On the form, click the Calculate button
  22. In the Properties window, click Image and click its ellipsis button
  23. In the Select Resource dialog box, click Import
  24. In the Files of Type box, select All Files
  25. Locate the folder of the current project, select Calculator.ico and click Open
  26. On the Select Resource dialog box, click OK
  27. Change the following properties of the Calculate button:
    ImageAlign: TopCenter
    TextAlign: BottomCenter
  28. On the form, click the Calculate button
  29. In the Properties window, click Image and click its ellipsis button
  30. In the Select Resource dialog box, click Import
  31. In the Files of Type box, select All Files
  32. Locate the folder of the current project, select exit.ico and click Open
  33. On the Select Resource dialog box, click OK
  34. Change the following properties of the Close button:
    ImageAlign: TopCenter
    TextAlign: BottomCenter
     
    Simple Interest
  35. On the form, double-click the Calculate button and implement its Click event as follows:
    private void btnCalculate_Click(object sender, EventArgs e)
    {
            decimal Principal    = 0.00M,
                        InterestRate = 0.00M,
                        InterestEarned,
                        FutureValue;
            decimal Periods = 0.00M;
    
            try
            {
                    Principal = decimal.Parse(txtPrincipal.Text);
            }
            catch (FormatException)
            {
                    MessageBox.Show("The value you entered for the " +
                                    "principal is not valid");
            }
    
            try
            {
                    InterestRate = decimal.Parse(txtInterestRate.Text);
            }
            catch (FormatException)
            {
                    MessageBox.Show("Wrong Value: The interest rate must " +
                                    "be a value between 0 and 100");
            }
    
            try
            {
                    Periods = decimal.Parse(txtPeriods.Text);
            }
            catch (FormatException)
            {
                    MessageBox.Show("You entered an invalid value for the periods");
            }
    
            decimal I = InterestRate / 100;
            decimal p = Periods / 12;
            InterestEarned = Principal * I  * p;
            FutureValue    = Principal + InterestEarned;
    
            txtInterestEarned.Text = InterestEarned.ToString("C");
            txtFutureValue.Text = FutureValue.ToString("C");
    }
  36. Return to the form
  37. On the form, double-click the Close button and implement its Click event as follows:
    private void btnClose_Click(object sender, EventArgs e)
    {
                Close();
    }
  38. Execute the application and test the calculation
     
    Interest Rate
  39. Close the form and return to your programming environment

Home Copyright © 2010-2020, FunctionX