C# Programming: From Problem Analysis to Program Design 4 edition (Chapter 10 PE
ID: 670450 • Letter: C
Question
C# Programming: From Problem Analysis to Program Design 4 edition (Chapter 10 PE 7 & 8)
7. The computer club is selling T-Shirts. Create an attractive user interface that allows users to select sizes (S, M, L, XL) and quantity. Which controls would be most appropriate? Remember, the fewer keystrokes required of the user the better. Display the selections made by the user with the Process menu option. Include an option to exit the application.
8. Add to your solution in Exercise 7 by including two more sizes, XSmall and XXLarge. Add statements that process the order by calculating the total cost. Each shirt is $16 except the XSmall and XXLarge; their specialty prices are $20 each. Allow users to purchase different sizes on the same order. Include an "Add to Cart" option from the Process menu that enables the user to add multiple selections to the order. Display the total cost for each selection and the final cost for the order. Include a Help menu option that displays instructions.
I have the GUI and most of the code. I need help with the "Add to Cart" click procedure and the total cost.
This is a Windows Forms Application
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 TShirtApp
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void menuExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void menuReset_Click(object sender, EventArgs e)
{
// reset form
cmboSize.SelectedIndex = -1;
txtBxQuantity.Text = "";
lblError.Text = "";
cmboSize.Focus();
}
private void menuProcess_Click(object sender, EventArgs e)
{
// process user selections
int quantity;
string selection;
if (cmboSize.SelectedIndex == -1)
{
lblError.Text = "Please select a size!";
}
quantity = GetQuantity();
selection =
"Size: " + cmboSize.SelectedItem +
" Quantity: " + quantity +
" Thank you!";
// Don't display output, if there is a problem with input
if (cmboSize.SelectedIndex != -1)
if (lblError.Text == "")
MessageBox.Show(selection, "Computer Club T-Shirts");
}
public int GetQuantity()
{
// validate data entered for quantity
int q;
if (int.TryParse(txtBxQuantity.Text, out q) == false)
{
lblError.Text = "Please enter only integers";
txtBxQuantity.SelectAll();
txtBxQuantity.Focus();
}
else if ((q < 1) || (q > 25))
{
lblError.Text = "Please enter a quantity between 1 and 25.";
txtBxQuantity.SelectAll();
}
return q;
}
private void cmboSize_MouseEnter(object sender, EventArgs e)
{
lblError.Text = "";
}
private void txtBxQuantity_TextChanged(object sender, EventArgs e)
{
lblError.Text = "";
}
private void menuInstruct_Click(object sender, EventArgs e)
{
MessageBox.Show("Selecting the 'Process' menu option will complete and place your order." +
" Selecting the 'Add to Cart' menu option will add your current selections to any previous selections made." +
" Selecting 'Reset' will reset your entire order." +
" *To order more than 25 t-shirts of each size, please see the club president for a special order form.",
"Computer Club T-Shirt Instructions");
}
}
*There is a label not visible titled lblError just above the text on the bottom of the GUI.
*The menu options: Process (should be like a complete or place order button), Add to Cart (should add what is selected on screen to the user's "cart"), Reset (already coded, may need updates), Exit.
namespace TShirtApp
{
partial class frmMain
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.cmboSize = new System.Windows.Forms.ComboBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.menuMain = new System.Windows.Forms.MenuStrip();
this.processToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.menuProcess = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
this.menuReset = new System.Windows.Forms.ToolStripMenuItem();
this.menuExit = new System.Windows.Forms.ToolStripMenuItem();
this.lblError = new System.Windows.Forms.Label();
this.txtBxQuantity = new System.Windows.Forms.TextBox();
this.toolStripSeparator3 = new System.Windows.Forms.ToolStripSeparator();
this.menuAddToCart = new System.Windows.Forms.ToolStripMenuItem();
this.helpToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.menuInstruct = new System.Windows.Forms.ToolStripMenuItem();
this.lblPrice = new System.Windows.Forms.Label();
this.menuMain.SuspendLayout();
this.SuspendLayout();
//
// cmboSize
//
this.cmboSize.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cmboSize.FormattingEnabled = true;
this.cmboSize.Items.AddRange(new object[] {
"XS",
"S",
"M",
"L",
"XL",
"XXL"});
this.cmboSize.Location = new System.Drawing.Point(194, 61);
this.cmboSize.Margin = new System.Windows.Forms.Padding(4);
this.cmboSize.Name = "cmboSize";
this.cmboSize.Size = new System.Drawing.Size(65, 26);
this.cmboSize.TabIndex = 0;
this.cmboSize.MouseEnter += new System.EventHandler(this.cmboSize_MouseEnter);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(85, 64);
this.label1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(58, 18);
this.label1.TabIndex = 2;
this.label1.Text = "Si&ze:";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(85, 106);
this.label2.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(98, 18);
this.label2.TabIndex = 3;
this.label2.Text = "&Quantity:";
//
// menuMain
//
this.menuMain.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.processToolStripMenuItem,
this.helpToolStripMenuItem});
this.menuMain.Location = new System.Drawing.Point(0, 0);
this.menuMain.Name = "menuMain";
this.menuMain.Padding = new System.Windows.Forms.Padding(4, 1, 0, 1);
this.menuMain.Size = new System.Drawing.Size(344, 24);
this.menuMain.TabIndex = 5;
this.menuMain.Text = "menuStrip1";
//
// processToolStripMenuItem
//
this.processToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.menuProcess,
this.menuAddToCart,
this.toolStripSeparator1,
this.menuReset,
this.toolStripSeparator3,
this.menuExit});
this.processToolStripMenuItem.Name = "processToolStripMenuItem";
this.processToolStripMenuItem.Size = new System.Drawing.Size(61, 22);
this.processToolStripMenuItem.Text = "&Options";
//
// menuProcess
//
this.menuProcess.Name = "menuProcess";
this.menuProcess.Size = new System.Drawing.Size(152, 22);
this.menuProcess.Text = "&Process";
this.menuProcess.Click += new System.EventHandler(this.menuProcess_Click);
//
// toolStripSeparator1
//
this.toolStripSeparator1.Name = "toolStripSeparator1";
this.toolStripSeparator1.Size = new System.Drawing.Size(149, 6);
//
// menuReset
//
this.menuReset.Name = "menuReset";
this.menuReset.Size = new System.Drawing.Size(152, 22);
this.menuReset.Text = "&Reset";
this.menuReset.Click += new System.EventHandler(this.menuReset_Click);
//
// menuExit
//
this.menuExit.Name = "menuExit";
this.menuExit.Size = new System.Drawing.Size(152, 22);
this.menuExit.Text = "E&xit";
this.menuExit.Click += new System.EventHandler(this.menuExit_Click);
//
// lblError
//
this.lblError.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
this.lblError.Location = new System.Drawing.Point(15, 143);
this.lblError.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.lblError.Name = "lblError";
this.lblError.Size = new System.Drawing.Size(316, 78);
this.lblError.TabIndex = 6;
this.lblError.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// txtBxQuantity
//
this.txtBxQuantity.Location = new System.Drawing.Point(194, 103);
this.txtBxQuantity.Name = "txtBxQuantity";
this.txtBxQuantity.Size = new System.Drawing.Size(65, 26);
this.txtBxQuantity.TabIndex = 7;
this.txtBxQuantity.TextChanged += new System.EventHandler(this.txtBxQuantity_TextChanged);
//
// toolStripSeparator3
//
this.toolStripSeparator3.Name = "toolStripSeparator3";
this.toolStripSeparator3.Size = new System.Drawing.Size(149, 6);
//
// menuAddToCart
//
this.menuAddToCart.Name = "menuAddToCart";
this.menuAddToCart.Size = new System.Drawing.Size(152, 22);
this.menuAddToCart.Text = "&Add to Cart";
//
// helpToolStripMenuItem
//
this.helpToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.menuInstruct});
this.helpToolStripMenuItem.Name = "helpToolStripMenuItem";
this.helpToolStripMenuItem.Size = new System.Drawing.Size(44, 22);
this.helpToolStripMenuItem.Text = "&Help";
//
// menuInstruct
//
this.menuInstruct.Name = "menuInstruct";
this.menuInstruct.Size = new System.Drawing.Size(152, 22);
this.menuInstruct.Text = "&Instructions";
this.menuInstruct.Click += new System.EventHandler(this.menuInstruct_Click);
//
// lblPrice
//
this.lblPrice.Font = new System.Drawing.Font("Lucida Sans Typewriter", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblPrice.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
this.lblPrice.Location = new System.Drawing.Point(13, 243);
this.lblPrice.Name = "lblPrice";
this.lblPrice.Size = new System.Drawing.Size(316, 34);
this.lblPrice.TabIndex = 8;
this.lblPrice.Text = "S, M, L, XL $16/ea. Specialty sizes XS, XXL $20/ea.";
this.lblPrice.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// frmMain
//
this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 18F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.LightSteelBlue;
this.ClientSize = new System.Drawing.Size(344, 286);
this.Controls.Add(this.lblPrice);
this.Controls.Add(this.txtBxQuantity);
this.Controls.Add(this.lblError);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.cmboSize);
this.Controls.Add(this.menuMain);
this.Font = new System.Drawing.Font("Lucida Sans Typewriter", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.ForeColor = System.Drawing.Color.DarkSlateBlue;
this.MainMenuStrip = this.menuMain;
this.Margin = new System.Windows.Forms.Padding(4);
this.MinimizeBox = false;
this.Name = "frmMain";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Computer Club T-Shirts";
this.menuMain.ResumeLayout(false);
this.menuMain.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.ComboBox cmboSize;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.MenuStrip menuMain;
private System.Windows.Forms.ToolStripMenuItem processToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem menuProcess;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator1;
private System.Windows.Forms.ToolStripMenuItem menuReset;
private System.Windows.Forms.ToolStripMenuItem menuExit;
private System.Windows.Forms.Label lblError;
private System.Windows.Forms.TextBox txtBxQuantity;
private System.Windows.Forms.ToolStripMenuItem menuAddToCart;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator3;
private System.Windows.Forms.ToolStripMenuItem helpToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem menuInstruct;
private System.Windows.Forms.Label lblPrice;
}
}
Explanation / Answer
writing click event is simple and inside click event we can add cart by clling getQuantity() method which was written already by you...which handling text box data is handled by this method..
private void Add_to_cart(object sender, System.EventArgs e)
{
this.cartItems = this.cartItems + GetQuantity();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.