Answer to Question #267649 in C# for Zocela

Question #267649

Create a Windows application where an attractive user interface that allows users to select sizes (S, M, L, XL) and quantity. Choose the most appropriate controls. 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. Include 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 speciality prices are $20 each Display the total cost of then selection. Include a help option that displays instructions. You can enhance your solution by allowing users to purchase different sizes on the same order.

1
Expert's answer
2021-11-17T15:03:32-0500
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.menuProcessOrder = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
            this.menuReset = new System.Windows.Forms.ToolStripMenuItem();
            this.toolStripSeparator3 = new System.Windows.Forms.ToolStripSeparator();
            this.menuExit = new System.Windows.Forms.ToolStripMenuItem();
            this.helpToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.menuInstruct = new System.Windows.Forms.ToolStripMenuItem();
            this.lblError = new System.Windows.Forms.Label();
            this.txtQuantity = new System.Windows.Forms.TextBox();
            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[] {
            "S",
            "M",
            "L",
            "XL",
            "XS",
            "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.menuProcessOrder,
            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(165, 22);
            this.menuProcess.Text = "&Process";
            this.menuProcess.Click += new System.EventHandler(this.menuProcess_Click);
            // 
            // menuProcessOrder
            // 
            this.menuProcessOrder.Name = "menuProcessOrder";
            this.menuProcessOrder.Size = new System.Drawing.Size(165, 22);
            this.menuProcessOrder.Text = "P&rocess the order";
            this.menuProcessOrder.Click += new System.EventHandler(this.menuProcessOrder_Click);
            // 
            // toolStripSeparator1
            // 
            this.toolStripSeparator1.Name = "toolStripSeparator1";
            this.toolStripSeparator1.Size = new System.Drawing.Size(162, 6);
            // 
            // menuReset
            // 
            this.menuReset.Name = "menuReset";
            this.menuReset.Size = new System.Drawing.Size(165, 22);
            this.menuReset.Text = "&Reset";
            this.menuReset.Click += new System.EventHandler(this.menuReset_Click);
            // 
            // toolStripSeparator3
            // 
            this.toolStripSeparator3.Name = "toolStripSeparator3";
            this.toolStripSeparator3.Size = new System.Drawing.Size(162, 6);
            // 
            // menuExit
            // 
            this.menuExit.Name = "menuExit";
            this.menuExit.Size = new System.Drawing.Size(165, 22);
            this.menuExit.Text = "E&xit";
            this.menuExit.Click += new System.EventHandler(this.menuExit_Click);
            // 
            // 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(136, 22);
            this.menuInstruct.Text = "&Instructions";
            this.menuInstruct.Click += new System.EventHandler(this.menuInstruct_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;
            // 
            // txtQuantity
            // 
            this.txtQuantity.Location = new System.Drawing.Point(194, 103);
            this.txtQuantity.Name = "txtQuantity";
            this.txtQuantity.Size = new System.Drawing.Size(65, 26);
            this.txtQuantity.TabIndex = 7;
            this.txtQuantity.TextChanged += new System.EventHandler(this.txtQuantity_TextChanged);
            // 
            // 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/each. \r\nSpecialty sizes XS, XXL $20/each.";
            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.SystemColors.ActiveBorder;
            this.ClientSize = new System.Drawing.Size(344, 286);
            this.Controls.Add(this.lblPrice);
            this.Controls.Add(this.txtQuantity);
            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 txtQuantity;
        private System.Windows.Forms.ToolStripMenuItem menuProcessOrder;
        private System.Windows.Forms.ToolStripSeparator toolStripSeparator3;
        private System.Windows.Forms.ToolStripMenuItem helpToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem menuInstruct;
        private System.Windows.Forms.Label lblPrice;
    }
}





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
    {
        struct TShirt
        {
            public string size;
            public double price;
            public int quantity;
            public double totalCost;
        }


        private List<TShirt> cart = new List<TShirt>();


        public frmMain()
        {
            InitializeComponent();
        }


        private void menuExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void menuReset_Click(object sender, EventArgs e)
        {


            cmboSize.SelectedIndex = -1;
            txtQuantity.Text = "";
            lblError.Text = "";
            cmboSize.Focus();
        }


        private void menuProcess_Click(object sender, EventArgs e)
        {
            int quantity;
            if (cmboSize.SelectedIndex == -1)
            {
                lblError.Text = "Please select a size!";
            }
            else
            {
                quantity = validateQuantity();
                TShirt tShirt = new TShirt();


                if (cmboSize.SelectedIndex >= 0 && cmboSize.SelectedIndex <= 3)
                {
                    tShirt.price = 16;
                }
                else
                {
                    tShirt.price = 20;
                }
                tShirt.size = cmboSize.SelectedItem.ToString(); 
                tShirt.quantity = quantity;
                tShirt.totalCost = quantity * tShirt.price;
                cart.Add(tShirt);
                MessageBox.Show("Size: " + cmboSize.SelectedItem +
            "\n\nQuantity: " + quantity +
            "\n\nThank you!", "Computer Club T-Shirts");


            }
        }


        public int validateQuantity()
        {


            int quantity;
            if (!int.TryParse(txtQuantity.Text, out quantity))
            {
                lblError.Text = "Please enter only integers";
                txtQuantity.SelectAll();
                txtQuantity.Focus();
            }


            else if ((quantity < 1) || (quantity > 25))
            {
                lblError.Text = "Please enter a quantity between 1 and 25.";
                txtQuantity.SelectAll();
            }
            return quantity;
        }


        private void cmboSize_MouseEnter(object sender, EventArgs e)
        {
            lblError.Text = "";
        }


        private void txtQuantity_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." +
            "\n\nSelecting the 'Add to Cart' menu option will add your current selections to any previous selections made." +
            "\n\nSelecting 'Reset' will reset your entire order." +
            "\n\n*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");
        }


        private void menuProcessOrder_Click(object sender, EventArgs e)
        {
            string report = "";
            double total=0;
            for (int i = 0; i < cart.Count; i++)
            {
                report += "Size: " + cart[i].size +
                    "\nQuantity: " + cart[i].quantity.ToString() +
                    "\nPrice: " + cart[i].price.ToString("N2") +
                    "\nSubtotal: " + cart[i].totalCost.ToString("N2") + "\n\n";
                total += cart[i].totalCost;
            }
            report += "Total: " + total.ToString("N2")+"\n";


            MessageBox.Show(report + "\n\nThank you!", "Computer Club T-Shirts. The total cost");
        }




    }


}








Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS