Visual C#

Example Application: Simple Interest

The date/time picker control provides a convenient way to select a date to use in an application. This reduces the likelihood of mistakes. To illustrate…

3 min read593 words

Description#

The date/time picker control provides a convenient way to select a date to use in an application. This reduces the likelihood of mistakes. To illustrate it, we will create an application that calculates the values of an interest paid on a loan. Application: Introducing the Application

  1. Start a new Window Application named SimpleInterest2
  2. 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 Loan Start Date: DateTimePicker dtpStartDate Label Loan End Date: DateTimePicker dtpLoandEndDate Label Periods: TextBox 1 txtPeriods Right Label days Button Calculate btnCalculate GroupBox Results Label Interest Earned: TextBox 0.00 txtInterestEarned Right Label Future Value: TextBox 0.00 txtFutureValue Right Button Close btnClose
  3. On the form, click the dtdEndDate date picker control
  4. In the Properties window, click the Events button and double-click CloseUp to generate its event
  5. Implement the event as follows: private void dtpEndDate_CloseUp(object sender, EventArgs e) { double Principal = 0.00D, InterestRate = 0.00D, Periods = 0.00D; double InterestEarned, FutureValue; DateTime StartDate, EndDate; TimeSpan spnPeriods; // Get the value of the principal try { Principal = double.Parse(txtPrincipal.Text); } catch (FormatException) { MessageBox.Show("Invalid Principal Value"); } // Get the interest rate try { InterestRate = double.Parse(txtInterestRate.Text) / 100; } catch (FormatException) { MessageBox.Show("Invalid Interest Rate"); } // Get the start and end dates StartDate = dtpStartDate.Value; EndDate = dtpEndDate.Value; // Make sure the end date doesn't occur before the start date if (EndDate < StartDate) { MessageBox.Show("Invalid Date Sequence: " + "the end date must occur after the start date"); dtpEndDate.Value = DateTime.Today; return; } // Get the difference in days // The will be the periods spnPeriods = EndDate.Subtract(StartDate); int days = spnPeriods.Days; txtPeriods.Text = days.ToString(); double p = 0.00D; // Because we will allow the user to directly specify // the number of days, let's get the period from its text box try { p = double.Parse(txtPeriods.Text); } catch (FormatException) { MessageBox.Show("Invalid number of days"); } // The actual period is gotten as follows Periods = p / 365; // Now we can perform the calculations InterestEarned = Principal * InterestRate * Periods; FutureValue = Principal + InterestEarned; // Display the values txtInterestEarned.Text = InterestEarned.ToString("C"); txtFutureValue.Text = FutureValue.ToString("C"); }
  6. Return to the form and click the Periods text box
  7. From the Events section of the Properties window, double-click Leave
  8. Implement the event as follows: private void txtPeriods_Leave(object sender, EventArgs e) { // If the user directly enters the number of days // in the Periods text box and press Tab, // we can perform the caltulation dtpEndDate_CloseUp(sender, e); }
  9. Return to the form and double-click the Calculate button
  10. Implement the event as follows: private void btnCalculate_Click(object sender, EventArgs e) { // If the user directly enters the number of days // in the Periods text box and clicks the Calculate // button, we can perform the caltulation dtpEndDate_CloseUp(sender, e); }
  11. Return to the form and double-click the Close button
  12. Implement the event as follows: private void btnClose_Click(object sender, EventArgs e) { Close(); }
  13. Return to the form
  14. In the Properties window, click the Properties button
  15. On the form, click the top date picker control
  16. In the Properties window, change the following values: CalendarForeColor: Blue CalendarMonthBackground: SkyBlue CalendarTitleBackColor: Navy CalendarTitleForeColor: Gold CalendarTrailingForeColor: DodgeBlue
  17. On the form, click the top date picker control
  18. In the Properties window, change the following values: CalendarForeColor: Maroon CalendarMonthBackground: Orange CalendarTitleBackColor: Sienna CalendarTitleForeColor: Khaki CalendarTrailingForeColor: White
  19. Execute the application and test the application
  20. Close the form and return to your programming environment

Home Copyright © 2010-2011 C#Key