Question
Enhance the Future Value App: 1. Generate the event handler for the Load event of the form. Then, add code that loads the numbers 1-20
Enhance the Future Value App:
1. Generate the event handler for the Load event of the form. Then, add code that loads the numbers 1-20 in the Number of Years combo box and add code that selects 3 as the default number of years.
2. Modify the event handler for the Click event of the Calculate button so it gets the number of years from the combo box and adds the future value for each year to the Future Values list box.
3. To get this to work correctly, you'll need to use the Clear method of the list box to clear the list box each time the Calculate button is clicked. In addition, you can use the modulus operator (%) to add the future value after every twelve months of the calculation. For example: if (month % 12 == 0) //add the future value to the list box
4. Test this application to make sure it works correctly.
This is what I have so far:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;
namespace FutureValue { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
// TODO: Declare the rectangular array and the row index here
private void btnCalculate_Click(object sender, EventArgs e) { try { if (IsValidData()) { decimal monthlyInvestment = Convert.ToDecimal(txtMonthlyInvestment.Text); decimal interestRateYearly = Convert.ToDecimal(txtInterestRate.Text); int years = Convert.ToInt32(txtYears.Text);
int months = years * 12; decimal interestRateMonthly = interestRateYearly / 12 / 100;
decimal futureValue = CalculateFutureValue( monthlyInvestment, interestRateMonthly, months); txtFutureValue.Text = futureValue.ToString("c"); txtMonthlyInvestment.Focus();
// TODO: Add the calculation to the rectangular array here } } catch (Exception ex) { MessageBox.Show(ex.Message + " " + ex.GetType().ToString() + " " + ex.StackTrace, "Exception"); } }
public bool IsValidData() { return // Validate the Monthly Investment text box IsPresent(txtMonthlyInvestment, "Monthly Investment") && IsDecimal(txtMonthlyInvestment, "Monthly Investment") && IsWithinRange(txtMonthlyInvestment, "Monthly Investment", 1, 1000) &&
// Validate the Yearly Interest Rate text box IsPresent(txtInterestRate, "Yearly Interest Rate") && IsDecimal(txtInterestRate, "Yearly Interest Rate") && IsWithinRange(txtInterestRate, "Yearly Interest Rate", 1, 20) &&
// Validate the Number of Years text box IsPresent(txtYears, "Number of Years") && IsInt32(txtYears, "Number of Years") && IsWithinRange(txtYears, "Number of Years", 1, 40); }
public bool IsPresent(TextBox textBox, string name) { if (textBox.Text == "") { MessageBox.Show(name + " is a required field.", "Entry Error"); textBox.Focus(); return false; } return true; }
public bool IsDecimal(TextBox textBox, string name) { decimal number = 0m; if (Decimal.TryParse(textBox.Text, out number)) { return true; } else { MessageBox.Show(name + " must be a decimal value.", "Entry Error"); textBox.Focus(); return false; } }
public bool IsInt32(TextBox textBox, string name) { int number = 0; if (Int32.TryParse(textBox.Text, out number)) { return true; } else { MessageBox.Show(name + " must be an integer.", "Entry Error"); textBox.Focus(); return false; } }
public bool IsWithinRange(TextBox textBox, string name, decimal min, decimal max) { decimal number = Convert.ToDecimal(textBox.Text); if (number < min || number > max) { MessageBox.Show(name + " must be between " + min + " and " + max + ".", "Entry Error"); textBox.Focus(); return false; } return true; }
private decimal CalculateFutureValue(decimal monthlyInvestment, decimal monthlyInterestRate, int months) { decimal futureValue = 0m; for (int i = 0; i < months; i++) { futureValue = (futureValue + monthlyInvestment) * (1 + monthlyInterestRate); }
return futureValue; }
private void btnExit_Click(object sender, EventArgs e) { // TODO: Display the rectangular array in a dialog box here this.Close(); }
private void Form1_Load(object sender, EventArgs e) {
} } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started