1 // Student: Pat Moss
2 // Instructor: Dave Hammer
3 // CIS162AD C# 5832 - Worksheet #8 - Form #2
4 // 04/09/2006 Rev. 01a Investment Calculator
5
6 // III. Programming Exercises: Write source code for the following:
7 // 2. Design and implement an application that contains GUI
8 // components and calculates an investment value. It should
9 // have text boxes that allow the user to enter the amount
10 // invested, the number of years for the investment, and the
11 // annual interest rate. When the user presses the button,
12 // it will accept the inputs and calculate the total value
13 // of the investment after the specified number of years.
14 // The answer should be written to a label.
15 // Turn in your source code for this program.
16
17 using System;
18 using System.Drawing;
19 using System.Collections;
20 using System.ComponentModel;
21 using System.Windows.Forms;
22 using System.Data;
23
24 namespace form2
25 {
26 ///
27 /// Summary description for Form2 form
28 ///
29 public class Form2 : System.Windows.Forms.Form
30 {
31 private System.Windows.Forms.Button btnCalculate;
32 private System.Windows.Forms.Button btnExit;
33 private System.Windows.Forms.Button btnClear;
34 private System.Windows.Forms.Button btnHelp;
35 private System.Windows.Forms.Label label1;
36 private System.Windows.Forms.Label label2;
37 private System.Windows.Forms.Label label3;
38 private System.Windows.Forms.Label label4;
39 private System.Windows.Forms.Label label5;
40 private System.Windows.Forms.Label label6;
41 private System.Windows.Forms.TextBox textAmtInvested;
42 private System.Windows.Forms.TextBox textNumYears;
43 private System.Windows.Forms.TextBox textIntRate;
44 private System.Windows.Forms.TextBox textInterest;
45 private System.Windows.Forms.TextBox textFinalValue;
46 private System.Windows.Forms.ToolTip toolTip1;
47 private System.ComponentModel.IContainer components;
48
49 public Form2()
50 {
51 //
52 // Required for Windows Form Designer support
53 //
54 InitializeComponent();
55
56 //
57 // TODO: Add any constructor code after InitializeComponent call
58 //
59 }
60
61 ///
62 /// Clean up any resources being used
63 ///
64 protected override void Dispose( bool disposing )
65 {
66 if( disposing )
67 {
68 if (components != null)
69 {
70 components.Dispose();
71 }
72 }
73 base.Dispose( disposing );
74 }
75
76 #region Windows Form Designer generated code
77 ///
78 /// Required method for Designer support - do not modify
79 /// the contents of this method with the code editor
80 ///
81 private void InitializeComponent()
82 {
83 this.components = new System.ComponentModel.Container();
84 this.btnCalculate = new System.Windows.Forms.Button();
85 this.btnExit = new System.Windows.Forms.Button();
86 this.btnClear = new System.Windows.Forms.Button();
87 this.btnHelp = new System.Windows.Forms.Button();
88 this.label1 = new System.Windows.Forms.Label();
89 this.label2 = new System.Windows.Forms.Label();
90 this.label3 = new System.Windows.Forms.Label();
91 this.label4 = new System.Windows.Forms.Label();
92 this.label5 = new System.Windows.Forms.Label();
93 this.textAmtInvested = new System.Windows.Forms.TextBox();
94 this.textNumYears = new System.Windows.Forms.TextBox();
95 this.textIntRate = new System.Windows.Forms.TextBox();
96 this.textInterest = new System.Windows.Forms.TextBox();
97 this.textFinalValue = new System.Windows.Forms.TextBox();
98 this.label6 = new System.Windows.Forms.Label();
99 this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
100 this.SuspendLayout();
101 //
102 // btnCalculate
103 //
104 this.btnCalculate.Location = new System.Drawing.Point(200, 192);
105 this.btnCalculate.Name = "btnCalculate";
106 this.btnCalculate.Size = new System.Drawing.Size(80, 24);
107 this.btnCalculate.TabIndex = 7;
108 this.btnCalculate.Text = "Calculate";
109 this.btnCalculate.Click += new System.EventHandler(this.btnCalculate_Click);
110 //
111 // btnExit
112 //
113 this.btnExit.Location = new System.Drawing.Point(296, 192);
114 this.btnExit.Name = "btnExit";
115 this.btnExit.Size = new System.Drawing.Size(80, 24);
116 this.btnExit.TabIndex = 8;
117 this.btnExit.Text = "Exit";
118 this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
119 //
120 // btnClear
121 //
122 this.btnClear.Location = new System.Drawing.Point(104, 192);
123 this.btnClear.Name = "btnClear";
124 this.btnClear.Size = new System.Drawing.Size(80, 24);
125 this.btnClear.TabIndex = 6;
126 this.btnClear.Text = "Clear";
127 this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
128 //
129 // btnHelp
130 //
131 this.btnHelp.Location = new System.Drawing.Point(8, 192);
132 this.btnHelp.Name = "btnHelp";
133 this.btnHelp.Size = new System.Drawing.Size(80, 24);
134 this.btnHelp.TabIndex = 5;
135 this.btnHelp.Text = "Help";
136 this.btnHelp.Click += new System.EventHandler(this.btnHelp_Click);
137 //
138 // label1
139 //
140 this.label1.Location = new System.Drawing.Point(16, 32);
141 this.label1.Name = "label1";
142 this.label1.Size = new System.Drawing.Size(112, 16);
143 this.label1.TabIndex = 9;
144 this.label1.Text = "Amount Invested";
145 this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
146 //
147 // label2
148 //
149 this.label2.Location = new System.Drawing.Point(16, 64);
150 this.label2.Name = "label2";
151 this.label2.Size = new System.Drawing.Size(112, 16);
152 this.label2.TabIndex = 10;
153 this.label2.Text = "Number of Years";
154 this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
155 //
156 // label3
157 //
158 this.label3.Location = new System.Drawing.Point(16, 96);
159 this.label3.Name = "label3";
160 this.label3.Size = new System.Drawing.Size(112, 16);
161 this.label3.TabIndex = 11;
162 this.label3.Text = "Annual Interest Rate";
163 this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
164 //
165 // label4
166 //
167 this.label4.Location = new System.Drawing.Point(16, 128);
168 this.label4.Name = "label4";
169 this.label4.Size = new System.Drawing.Size(112, 16);
170 this.label4.TabIndex = 12;
171 this.label4.Text = "Total Interest";
172 this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
173 //
174 // label5
175 //
176 this.label5.Location = new System.Drawing.Point(16, 160);
177 this.label5.Name = "label5";
178 this.label5.Size = new System.Drawing.Size(112, 16);
179 this.label5.TabIndex = 13;
180 this.label5.Text = "Final Value";
181 this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
182 //
183 // textAmtInvested
184 //
185 this.textAmtInvested.Location = new System.Drawing.Point(136, 32);
186 this.textAmtInvested.Name = "textAmtInvested";
187 this.textAmtInvested.Size = new System.Drawing.Size(192, 20);
188 this.textAmtInvested.TabIndex = 0;
189 this.textAmtInvested.Text = "";
190 this.textAmtInvested.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
191 this.toolTip1.SetToolTip(this.textAmtInvested, "Amount Invested: Enter 1 to 1000000");
192 //
193 // textNumYears
194 //
195 this.textNumYears.Location = new System.Drawing.Point(136, 64);
196 this.textNumYears.Name = "textNumYears";
197 this.textNumYears.Size = new System.Drawing.Size(192, 20);
198 this.textNumYears.TabIndex = 1;
199 this.textNumYears.Text = "";
200 this.textNumYears.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
201 this.toolTip1.SetToolTip(this.textNumYears, "Number of Years: Enter 1 to 100");
202 //
203 // textIntRate
204 //
205 this.textIntRate.Location = new System.Drawing.Point(136, 96);
206 this.textIntRate.Name = "textIntRate";
207 this.textIntRate.Size = new System.Drawing.Size(192, 20);
208 this.textIntRate.TabIndex = 2;
209 this.textIntRate.Text = "";
210 this.textIntRate.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
211 this.toolTip1.SetToolTip(this.textIntRate, "Annual Interest Rate: Enter 0.01 to 0.50");
212 //
213 // textInterest
214 //
215 this.textInterest.Location = new System.Drawing.Point(136, 128);
216 this.textInterest.Name = "textInterest";
217 this.textInterest.ReadOnly = true;
218 this.textInterest.Size = new System.Drawing.Size(192, 20);
219 this.textInterest.TabIndex = 3;
220 this.textInterest.Text = "";
221 this.textInterest.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
222 //
223 // textFinalValue
224 //
225 this.textFinalValue.Location = new System.Drawing.Point(136, 160);
226 this.textFinalValue.Name = "textFinalValue";
227 this.textFinalValue.ReadOnly = true;
228 this.textFinalValue.Size = new System.Drawing.Size(192, 20);
229 this.textFinalValue.TabIndex = 4;
230 this.textFinalValue.Text = "";
231 this.textFinalValue.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
232 //
233 // label6
234 //
235 this.label6.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
236 this.label6.Location = new System.Drawing.Point(128, 8);
237 this.label6.Name = "label6";
238 this.label6.Size = new System.Drawing.Size(200, 16);
239 this.label6.TabIndex = 14;
240 this.label6.Text = "Investment Calculator";
241 this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
242 //
243 // Form2
244 //
245 this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
246 this.ClientSize = new System.Drawing.Size(392, 238);
247 this.Controls.Add(this.label6);
248 this.Controls.Add(this.textFinalValue);
249 this.Controls.Add(this.textInterest);
250 this.Controls.Add(this.textIntRate);
251 this.Controls.Add(this.textNumYears);
252 this.Controls.Add(this.textAmtInvested);
253 this.Controls.Add(this.label5);
254 this.Controls.Add(this.label4);
255 this.Controls.Add(this.label3);
256 this.Controls.Add(this.label2);
257 this.Controls.Add(this.label1);
258 this.Controls.Add(this.btnHelp);
259 this.Controls.Add(this.btnClear);
260 this.Controls.Add(this.btnExit);
261 this.Controls.Add(this.btnCalculate);
262 this.Name = "Form2";
263 this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
264 this.Text = "Worksheet #8 - Form #2 - by Pat Moss";
265 this.Load += new System.EventHandler(this.Form2_Load);
266 this.ResumeLayout(false);
267
268 }
269 #endregion
270
271 ///
272 /// The main entry point for the application
273 ///
274 [STAThread]
275 static void Main()
276 {
277 Application.Run(new Form2());
278 }
279
280 // Form load event
281 // Initialize the screen controls
282 private void Form2_Load(object sender, System.EventArgs e)
283 {
284 InitScreenControls();
285 }
286
287 // "Help" button event
288 // The user has clicked on the "Help" button
289 // Display user help information
290 private void btnHelp_Click(object sender, System.EventArgs e)
291 {
292 string helpinfo = "";
293 helpinfo += " User Instructions:\n\n";
294 helpinfo += "1. Enter an Initial Investment Amount (1 to 1000000).\n";
295 helpinfo += "2. Enter the Number of Years (1 to 100).\n";
296 helpinfo += "3. Enter the Annual Interest Rate (0.10 to 0.50).\n";
297 helpinfo += "4. Then click on Calculate.\n";
298 helpinfo += "5. View the Total Interest and the Final Value.\n";
299 MessageBox.Show(helpinfo, "Form2 Help",
300 MessageBoxButtons.OK, MessageBoxIcon.Information,
301 MessageBoxDefaultButton.Button1);
302 }
303
304 // "Clear" button event
305 // The user has clicked on the "Clear" button
306 // Initialize the screen controls
307 private void btnClear_Click(object sender, System.EventArgs e)
308 {
309 InitScreenControls();
310 }
311
312 // Initialize the screen text box controls
313 private void InitScreenControls()
314 {
315 textAmtInvested.Text = "";
316 textNumYears.Text = "";
317 textIntRate.Text = "";
318 textInterest.Text = "";
319 textFinalValue.Text = "";
320 textAmtInvested.Focus();
321 }
322
323 // "Calculate" button event
324 // The user has clicked on the "Calculate" button
325 // so calculate the Total Interest and Final Value.
326 // Then display the calculated values on the form.
327 private void btnCalculate_Click(object sender, System.EventArgs e)
328 {
329 string strAmtInvested = "";
330 string strNumYears = "";
331 string strIntRate = "";
332
333 double dblAmtInvested = 0;
334 int intNumYears = 0;
335 double dblIntRate = 0;
336 double dblInterest = 0;
337 double dblFinalValue = 0;
338
339 bool inperror = false;
340
341 // fetch working-storage string values
342 // from the input text boxes
343 strAmtInvested = textAmtInvested.Text;
344 strNumYears = textNumYears.Text;
345 strIntRate = textIntRate.Text;
346
347 // convert the "amount invested" string to a double
348 try
349 {
350 dblAmtInvested = double.Parse(strAmtInvested);
351 }
352 catch
353 {
354 if (!inperror)
355 {
356 string helpinfo = "Amount Invested: Please enter a dollar value: ";
357 helpinfo += "1 to 100000.";
358 MessageBox.Show(helpinfo, "Bad Input Data",
359 MessageBoxButtons.OK, MessageBoxIcon.Error,
360 MessageBoxDefaultButton.Button1);
361 textAmtInvested.Focus();
362 }
363
364 inperror = true;
365 }
366
367 // convert the "number of years" string to an integer
368 try
369 {
370 intNumYears = int.Parse(strNumYears);
371 }
372 catch
373 {
374 if (!inperror)
375 {
376 string helpinfo = "Number of Years: Please enter an integer value: ";
377 helpinfo += "1 to 100.";
378 MessageBox.Show(helpinfo, "Bad Input Data",
379 MessageBoxButtons.OK, MessageBoxIcon.Error,
380 MessageBoxDefaultButton.Button1);
381 textNumYears.Focus();
382 }
383
384 inperror = true;
385 }
386
387 // convert the "interest rate" string to a double
388 try
389 {
390 dblIntRate = double.Parse(strIntRate);
391 }
392 catch
393 {
394 if (!inperror)
395 {
396 string helpinfo = "Interest Rate: Please enter an interest rate: ";
397 helpinfo += "0.01 to 0.50";
398 MessageBox.Show(helpinfo, "Bad Input Data",
399 MessageBoxButtons.OK, MessageBoxIcon.Error,
400 MessageBoxDefaultButton.Button1);
401 textIntRate.Focus();
402 }
403
404 inperror = true;
405 }
406
407 // check the Amount Invested range $1 to $1,000,000
408 if ((dblAmtInvested < 1.00) || (dblAmtInvested > 1000000))
409 {
410 if (!inperror)
411 {
412 string helpinfo = "Amount Invested: Please enter 1 to 1000000.";
413 MessageBox.Show(helpinfo, "Bad Input Data",
414 MessageBoxButtons.OK, MessageBoxIcon.Error,
415 MessageBoxDefaultButton.Button1);
416 textAmtInvested.Focus();
417 }
418
419 inperror = true;
420 }
421
422 // check the Number of Years range 1 to 100
423 if ((intNumYears < 1) || (intNumYears > 100))
424 {
425 if (!inperror)
426 {
427 string helpinfo = "Number of Years: Please enter 1 to 100.";
428 MessageBox.Show(helpinfo, "Bad Input Data",
429 MessageBoxButtons.OK, MessageBoxIcon.Error,
430 MessageBoxDefaultButton.Button1);
431 textNumYears.Focus();
432 }
433
434 inperror = true;
435 }
436
437 // check the Annual Interest Rate range 0.01 to 0.50
438 if ((dblIntRate < 0.01) || (dblIntRate > 0.50))
439 {
440 if (!inperror)
441 {
442 string helpinfo = "Annual Interest Rate: Please enter 0.01 to 0.50.";
443 MessageBox.Show(helpinfo, "Bad Input Data",
444 MessageBoxButtons.OK, MessageBoxIcon.Error,
445 MessageBoxDefaultButton.Button1);
446 textIntRate.Focus();
447 }
448
449 inperror = true;
450 }
451
452 // if input data is ok, then calculate and display
453 // the Total Interest and Final Value results now
454 if (!inperror)
455 {
456 // round AmtInvested and IntRate values to two decimal places
457 dblAmtInvested = Math.Round(dblAmtInvested, 2);
458 dblIntRate = Math.Round(dblIntRate, 2);
459
460 // "clean up" the display format for input values on the form, with
461 // a. AmtInvested displayed to two decimal places (1.00 to 1000000.00)
462 // b. NumYears displayed as an integer (1 to 100)
463 // c. IntRate displayed to two decimal places (0.00 to 0.50)
464 textAmtInvested.Text = dblAmtInvested.ToString("######0.00");
465 textNumYears.Text = intNumYears.ToString("##0");
466 textIntRate.Text = dblIntRate.ToString("0.00");
467
468 double dblBgnPrin = dblAmtInvested;
469 double dblEndPrin = 0;
470 double dblBgnInt = 0;
471 double dblEndInt = 0;
472 double dblYearInt = 0;
473 int yearnum = 0;
474
475 // determine the annual interest for each year, and
476 // accumulate total interest and total final value
477 for (yearnum = 1; yearnum<=intNumYears; yearnum++)
478 {
479 dblYearInt = dblBgnPrin * dblIntRate;
480 dblEndInt = dblBgnInt + dblYearInt;
481 dblEndPrin = dblBgnPrin + dblYearInt;
482
483 dblBgnPrin = dblEndPrin;
484 dblBgnInt = dblEndInt;
485 }
486
487 // round the total Interest and FinalValue to two decimal places
488 dblInterest = Math.Round(dblEndInt, 2);
489 dblFinalValue = Math.Round(dblEndPrin, 2);
490
491 // display total Interest and FinalValue to the form as dollar currency
492 textInterest.Text = dblInterest.ToString("c");
493 textFinalValue.Text = dblFinalValue.ToString("c");
494 }
495 }
496
497 // "Exit" button event
498 // The user has clicked on the "Exit" button
499 // so exit from this application now.
500 private void btnExit_Click(object sender, System.EventArgs e)
501 {
502 this.Close();
503 }
504 }
505 }
506