- Inside the InvestmentForm Class
- The btnOK_Click() Method
- Summary
The btnOK_Click() Method
I promised to get back to the btnOK_Click() method, so here it is:
private void btnOK_Click(object sender, System.EventArgs e) { ic.Principal = Convert.ToDecimal(txtPrincipal.Text); ic.Interest = Convert.ToSingle(txtInterest.Text); ic.Months = Convert.ToInt32(txtMonths.Text); decimal estimate = ic.EstimateReturn(); string results = string.Format("Estimated Value: {0:C}", estimate); MessageBox.Show(results); }
This method extracts the data from each of the text box controls by reading their Text property. Each of the values must be converted from their string representations to the appropriate type with the appropriate static Convert class method. After the values are assigned to the InvestmentController properties, the EstimateReturn() method is invoked.
The string type has a format method that works just like the parameters to the Console.WriteLine() method calls in Part I. A formatted string is then created and passed as a parameter to the MessageBox.Show() method. This makes a message box appear on the screen, indicating the estimated result of an investment. Figure 2 shows whatthe message box with results looks like.
Figure 2 A message box with program results.