1 // Student: Pat Moss
2 // Instructor: Dave Hammer
3 // CIS162AD C# 5832 - Worksheet #6 - Carpet Cost Calculator
4 // 03/25/2006 Rev. 01a (includes try/catch blocks and Help)
5
6 // Programming exercise #6 on page 553:
7 // Create a Windows application for purchasing carpet.
8 // Allow the length and width of a room to be entered.
9 // Have a control that displays different prices of carpet.
10 // Include, for example, prices such as $21.95 per square yard.
11 // After the user enters their room dimensions and the price
12 // of the carpet, display the total cost to carpet the room.
13 // Ref: "Visual C# .NET Programming", B Doyle, 0619159979.
14 using System;
15 using System.Drawing;
16 using System.Collections;
17 using System.ComponentModel;
18 using System.Windows.Forms;
19 using System.Data;
20
21 namespace carpetcalculator
22 {
23 ///
24 /// Summary description for CarpetCalculator form
25 ///
26 public class CarpetCalculator : System.Windows.Forms.Form
27 {
28 private System.Windows.Forms.ComboBox cboPrices;
29 private System.Windows.Forms.TextBox txtWidth;
30 private System.Windows.Forms.TextBox txtLength;
31 private System.Windows.Forms.Label label1;
32 private System.Windows.Forms.Label label2;
33 private System.Windows.Forms.Button btnCalculate;
34 private System.Windows.Forms.Button btnExit;
35 private System.Windows.Forms.Label label3;
36 private System.Windows.Forms.Label label4;
37 private System.Windows.Forms.TextBox txtTotalCost;
38 private System.Windows.Forms.Label label5;
39 private System.Windows.Forms.TextBox txtSqYd;
40 private System.Windows.Forms.Button btnClear;
41 private System.Windows.Forms.Button btnHelp;
42
43 ///
44 /// Required designer variable
45 ///
46 private System.ComponentModel.Container components = null;
47
48 public CarpetCalculator()
49 {
50 //
51 // Required for Windows Form Designer support
52 //
53 InitializeComponent();
54
55 //
56 // TODO: Add any constructor code after InitializeComponent call
57 //
58 }
59
60 ///
61 /// Clean up any resources being used
62 ///
63 protected override void Dispose( bool disposing )
64 {
65 if( disposing )
66 {
67 if (components != null)
68 {
69 components.Dispose();
70 }
71 }
72 base.Dispose( disposing );
73 }
74
75 #region Windows Form Designer generated code
76 ///
77 /// Required method for Designer support - do not modify
78 /// the contents of this method with the code editor
79 ///
80 private void InitializeComponent()
81 {
82 this.cboPrices = new System.Windows.Forms.ComboBox();
83 this.txtWidth = new System.Windows.Forms.TextBox();
84 this.txtLength = new System.Windows.Forms.TextBox();
85 this.label1 = new System.Windows.Forms.Label();
86 this.label2 = new System.Windows.Forms.Label();
87 this.btnCalculate = new System.Windows.Forms.Button();
88 this.btnExit = new System.Windows.Forms.Button();
89 this.label3 = new System.Windows.Forms.Label();
90 this.txtSqYd = new System.Windows.Forms.TextBox();
91 this.label4 = new System.Windows.Forms.Label();
92 this.txtTotalCost = new System.Windows.Forms.TextBox();
93 this.label5 = new System.Windows.Forms.Label();
94 this.btnClear = new System.Windows.Forms.Button();
95 this.btnHelp = new System.Windows.Forms.Button();
96 this.SuspendLayout();
97 //
98 // cboPrices
99 //
100 this.cboPrices.Items.AddRange(new object[] {
101 "Choose a value",
102 "5.50",
103 "10.25",
104 "14.75",
105 "18.20",
106 "21.95",
107 "25.00"});
108 this.cboPrices.Location = new System.Drawing.Point(8, 32);
109 this.cboPrices.Name = "cboPrices";
110 this.cboPrices.Size = new System.Drawing.Size(112, 21);
111 this.cboPrices.TabIndex = 0;
112 //
113 // txtWidth
114 //
115 this.txtWidth.Location = new System.Drawing.Point(136, 32);
116 this.txtWidth.Name = "txtWidth";
117 this.txtWidth.Size = new System.Drawing.Size(72, 20);
118 this.txtWidth.TabIndex = 1;
119 this.txtWidth.Text = "";
120 this.txtWidth.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
121 //
122 // txtLength
123 //
124 this.txtLength.Location = new System.Drawing.Point(224, 32);
125 this.txtLength.Name = "txtLength";
126 this.txtLength.Size = new System.Drawing.Size(72, 20);
127 this.txtLength.TabIndex = 2;
128 this.txtLength.Text = "";
129 this.txtLength.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
130 //
131 // label1
132 //
133 this.label1.Location = new System.Drawing.Point(144, 8);
134 this.label1.Name = "label1";
135 this.label1.Size = new System.Drawing.Size(64, 16);
136 this.label1.TabIndex = 3;
137 this.label1.Text = "Width";
138 this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
139 //
140 // label2
141 //
142 this.label2.Location = new System.Drawing.Point(224, 8);
143 this.label2.Name = "label2";
144 this.label2.Size = new System.Drawing.Size(64, 16);
145 this.label2.TabIndex = 4;
146 this.label2.Text = "Length";
147 this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
148 //
149 // btnCalculate
150 //
151 this.btnCalculate.Location = new System.Drawing.Point(256, 80);
152 this.btnCalculate.Name = "btnCalculate";
153 this.btnCalculate.Size = new System.Drawing.Size(80, 24);
154 this.btnCalculate.TabIndex = 5;
155 this.btnCalculate.Text = "Calculate";
156 this.btnCalculate.Click += new System.EventHandler(this.btnCalculate_Click);
157 //
158 // btnExit
159 //
160 this.btnExit.Location = new System.Drawing.Point(352, 80);
161 this.btnExit.Name = "btnExit";
162 this.btnExit.Size = new System.Drawing.Size(80, 24);
163 this.btnExit.TabIndex = 6;
164 this.btnExit.Text = "Exit";
165 this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
166 //
167 // label3
168 //
169 this.label3.Location = new System.Drawing.Point(312, 8);
170 this.label3.Name = "label3";
171 this.label3.Size = new System.Drawing.Size(64, 16);
172 this.label3.TabIndex = 8;
173 this.label3.Text = "Sq. Yd.";
174 this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
175 //
176 // txtSqYd
177 //
178 this.txtSqYd.Location = new System.Drawing.Point(312, 32);
179 this.txtSqYd.Name = "txtSqYd";
180 this.txtSqYd.ReadOnly = true;
181 this.txtSqYd.Size = new System.Drawing.Size(72, 20);
182 this.txtSqYd.TabIndex = 9;
183 this.txtSqYd.TabStop = false;
184 this.txtSqYd.Text = "";
185 this.txtSqYd.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
186 //
187 // label4
188 //
189 this.label4.Location = new System.Drawing.Point(408, 8);
190 this.label4.Name = "label4";
191 this.label4.Size = new System.Drawing.Size(64, 16);
192 this.label4.TabIndex = 10;
193 this.label4.Text = "Total Cost";
194 this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
195 //
196 // txtTotalCost
197 //
198 this.txtTotalCost.Location = new System.Drawing.Point(400, 32);
199 this.txtTotalCost.Name = "txtTotalCost";
200 this.txtTotalCost.ReadOnly = true;
201 this.txtTotalCost.Size = new System.Drawing.Size(88, 20);
202 this.txtTotalCost.TabIndex = 11;
203 this.txtTotalCost.TabStop = false;
204 this.txtTotalCost.Text = "";
205 this.txtTotalCost.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
206 //
207 // label5
208 //
209 this.label5.Location = new System.Drawing.Point(16, 8);
210 this.label5.Name = "label5";
211 this.label5.Size = new System.Drawing.Size(88, 16);
212 this.label5.TabIndex = 12;
213 this.label5.Text = "Cost per Sq. Yd.";
214 this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
215 //
216 // btnClear
217 //
218 this.btnClear.Location = new System.Drawing.Point(160, 80);
219 this.btnClear.Name = "btnClear";
220 this.btnClear.Size = new System.Drawing.Size(80, 24);
221 this.btnClear.TabIndex = 13;
222 this.btnClear.Text = "Clear";
223 this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
224 //
225 // btnHelp
226 //
227 this.btnHelp.Location = new System.Drawing.Point(64, 80);
228 this.btnHelp.Name = "btnHelp";
229 this.btnHelp.Size = new System.Drawing.Size(80, 24);
230 this.btnHelp.TabIndex = 14;
231 this.btnHelp.Text = "Help";
232 this.btnHelp.Click += new System.EventHandler(this.btnHelp_Click);
233 //
234 // CarpetCalculator
235 //
236 this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
237 this.ClientSize = new System.Drawing.Size(496, 118);
238 this.Controls.Add(this.btnHelp);
239 this.Controls.Add(this.btnClear);
240 this.Controls.Add(this.label5);
241 this.Controls.Add(this.txtTotalCost);
242 this.Controls.Add(this.label4);
243 this.Controls.Add(this.txtSqYd);
244 this.Controls.Add(this.label3);
245 this.Controls.Add(this.btnExit);
246 this.Controls.Add(this.btnCalculate);
247 this.Controls.Add(this.label2);
248 this.Controls.Add(this.label1);
249 this.Controls.Add(this.txtLength);
250 this.Controls.Add(this.txtWidth);
251 this.Controls.Add(this.cboPrices);
252 this.Name = "CarpetCalculator";
253 this.Text = "Worksheet 06 - Carpet Cost Calculator - by Pat Moss";
254 this.Load += new System.EventHandler(this.CarpetCalculator_Load);
255 this.ResumeLayout(false);
256
257 }
258 #endregion
259
260 ///
261 /// The main entry point for the application
262 ///
263 [STAThread]
264 static void Main()
265 {
266 Application.Run(new CarpetCalculator());
267 }
268
269 // Form load event
270 // Initialize the screen controls
271 private void CarpetCalculator_Load(object sender, System.EventArgs e)
272 {
273 InitScreenControls();
274 }
275
276 // "Help" button event
277 // The user has clicked on the "Help" button
278 // Display user help information
279 private void btnHelp_Click(object sender, System.EventArgs e)
280 {
281 string helpinfo = "";
282 helpinfo += "1. Choose a Cost per Sq. Yd.\n";
283 helpinfo += "2. Enter a Width in yards.\n";
284 helpinfo += "3. Enter a Length in yards.\n";
285 helpinfo += "4. Click on Calculate.\n";
286 MessageBox.Show(helpinfo, "Carpet Cost Calculator Help",
287 MessageBoxButtons.OK, MessageBoxIcon.Information,
288 MessageBoxDefaultButton.Button1);
289 }
290
291 // "Clear" button event
292 // The user has clicked on the "Clear" button
293 // Initialize the screen controls
294 private void btnClear_Click(object sender, System.EventArgs e)
295 {
296 InitScreenControls();
297 }
298
299 // Initialize the screen controls
300 private void InitScreenControls()
301 {
302 cboPrices.SelectedIndex = 0;
303 txtWidth.Text = "0";
304 txtLength.Text = "0";
305 txtSqYd.Text = "0";
306 txtTotalCost.Text = "$0.00";
307 }
308
309 // "Calculate" button event
310 // The user has clicked on the "Calculate" button
311 // so calculate the square yardage and the total carpet cost.
312 // Use try/catch blocks to trap for non-numeric input data errors.
313 // Round each extended amount to two decimal places.
314 // Display the SqYd and Carpet Cost amounts on the screen.
315 private void btnCalculate_Click(object sender, System.EventArgs e)
316 {
317 string strPrice = "";
318 string strWidth = "";
319 string strLength = "";
320
321 double dblPrice = 0;
322 double dblWidth = 0;
323 double dblLength = 0;
324 double dblSqYd = 0;
325 double dblTotal = 0;
326
327 bool inperror = false;
328
329 strPrice = cboPrices.SelectedItem.ToString();
330 strWidth = txtWidth.Text;
331 strLength = txtLength.Text;
332
333 txtSqYd.Text = "0";
334 txtTotalCost.Text = "$0.00";
335
336 try
337 {
338 dblPrice = double.Parse(strPrice);
339 }
340 catch
341 {
342 string helpinfo = "Cost per Sq. Yd.: Please choose a value.";
343 MessageBox.Show(helpinfo, "Bad Input Data",
344 MessageBoxButtons.OK, MessageBoxIcon.Error,
345 MessageBoxDefaultButton.Button1);
346 cboPrices.SelectedIndex = 0;
347 cboPrices.Focus();
348 inperror = true;
349 }
350
351 try
352 {
353 dblWidth = double.Parse(strWidth);
354 }
355 catch
356 {
357 string helpinfo = "Width: Please enter a numeric value.";
358 MessageBox.Show(helpinfo, "Bad Input Data",
359 MessageBoxButtons.OK, MessageBoxIcon.Error,
360 MessageBoxDefaultButton.Button1);
361 if (!inperror) txtWidth.Focus();
362 inperror = true;
363 }
364
365 try
366 {
367 dblLength = double.Parse(strLength);
368 }
369 catch
370 {
371 string helpinfo = "Length: Please enter a numeric value.";
372 MessageBox.Show(helpinfo, "Bad Input Data",
373 MessageBoxButtons.OK, MessageBoxIcon.Error,
374 MessageBoxDefaultButton.Button1);
375 if (!inperror) txtLength.Focus();
376 inperror = true;
377 }
378
379 if (!inperror)
380 {
381 dblPrice = Math.Round(dblPrice, 2);
382 dblWidth = Math.Round(dblWidth, 2);
383 dblLength = Math.Round(dblLength, 2);
384
385 dblSqYd = dblWidth * dblLength;
386 dblSqYd = Math.Round(dblSqYd, 2);
387
388 dblTotal = dblPrice * dblSqYd;
389 dblTotal = Math.Round(dblTotal, 2);
390
391 txtSqYd.Text = dblSqYd.ToString();
392 txtTotalCost.Text = dblTotal.ToString("c");
393 }
394 }
395
396 // "Exit" button event
397 // The user has clicked on the "Exit" button
398 // so exit from this application now.
399 private void btnExit_Click(object sender, System.EventArgs e)
400 {
401 this.Close();
402 }
403 }
404 }