Home

Control-Based Application: Simple Interest

 

Description

This application was created to experiment some windows controls, namely:

The application is used to calculate the interest applied to a loan. It requests the principal, the interest rate, and the period as a number of months.

Practical Learning Practical Learning: Creating the Application

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

Home Copyright 2008-2009, yevol.com