Home

XML Example Application: FunDS (Fun Department Store)

   

Introduction

XML provide effective and wonderful means to create a database repository. XML by itself doesn't create or save records. It only defines how datashould be stored. On the other hand, another language, a library, or a programming environment can be used to create, save, or maintain data (for example, editing or deleting records).

We are going to create an example database application for a department store. We will name the business as FunDS, which stands for Fun Department Store.

The Employees

Naturally, we need people who can run the business.

Practical LearningPractical Learning: Introducing the Application

  1. Start a text editor such as Notepad and type the following:
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    public class EmployeeNew : Form
    {
        private Label lblEmployeeNumber;
        public TextBox txtEmployeeNumber;
        private Label lblFirstName;
        public TextBox txtFirstName;
        private Label lblLastName;
        public TextBox txtLastName;
        private Label lblAddress;
        public TextBox txtAddress;
        private Label lblCity;
        public TextBox txtCity;
        private Label lblCounty;
        public TextBox txtCounty;
        private Label lblState;
        public TextBox txtState;
        private Label lblZIPCode;
        public TextBox txtZIPCode;
        private Label lblMaritalStatus;
        public ComboBox cbxMaritalsStatus;
        private Label lblExemptions;
        public TextBox txtExemptions;
        private Label lblHourlySalary;
        public TextBox txtHourlySalary;
        private Button btnOK;
        private Button btnCancel;
    
        public EmployeeNew()
        {
            InitializeComponent();
        }
    
        private void InitializeComponent()
        {
            // Label: Employee Number
            lblEmployeeNumber = new Label();
            lblEmployeeNumber.AutoSize = true;
            lblEmployeeNumber.Location = new System.Drawing.Point(13, 18);
            lblEmployeeNumber.TabIndex = 0;
            lblEmployeeNumber.Text = "Employee #:";
            Controls.Add(lblEmployeeNumber);
    
            // Text Box: Employee Number
            txtEmployeeNumber = new TextBox();
            txtEmployeeNumber.Location = new System.Drawing.Point(94, 16);
            txtEmployeeNumber.Size = new System.Drawing.Size(88, 20);
            txtEmployeeNumber.TabIndex = 1;
            Controls.Add(txtEmployeeNumber);
    
            // Label: First Name
            lblFirstName = new Label();
            lblFirstName.AutoSize = true;
            lblFirstName.Location = new System.Drawing.Point(13, 49);
            lblFirstName.TabIndex = 2;
            lblFirstName.Text = "First Name:";
            Controls.Add(lblFirstName);
    
            // Text Box: First Name
            txtFirstName = new TextBox();
            txtFirstName.Location = new System.Drawing.Point(94, 46);
            txtFirstName.Size = new System.Drawing.Size(88, 20);
            txtFirstName.TabIndex = 3;
            Controls.Add(txtFirstName);
    
            // Label: Last Name
            lblLastName = new Label();
            lblLastName.AutoSize = true;
            lblLastName.Location = new System.Drawing.Point(195, 49);
            lblLastName.TabIndex = 4;
            lblLastName.Text = "Last Name:";
            Controls.Add(lblLastName);
    
            // Text Box: Last Name
            txtLastName = new TextBox();
            txtLastName.Location = new System.Drawing.Point(276, 46);
            txtLastName.Size = new System.Drawing.Size(88, 20);
            txtLastName.TabIndex = 5;
            Controls.Add(txtLastName);
    
            // Label: Address
            lblAddress = new Label();
            lblAddress.AutoSize = true;
            lblAddress.Location = new System.Drawing.Point(13, 75);
            lblAddress.TabIndex = 6;
            lblAddress.Text = "Address:";
            Controls.Add(lblAddress);
    
            // Text Box: Address
            txtAddress = new TextBox();
            txtAddress.Location = new System.Drawing.Point(94, 72);
            txtAddress.Size = new System.Drawing.Size(270, 20);
            txtAddress.TabIndex = 7;
            Controls.Add(txtAddress);
    
            // Label: City
            lblCity = new Label();
            lblCity.AutoSize = true;
            lblCity.Location = new System.Drawing.Point(13, 101);
            lblCity.TabIndex = 8;
            lblCity.Text = "City:";
            Controls.Add(lblCity);
    
            // Text Box: City
            txtCity = new TextBox();
            txtCity.Location = new System.Drawing.Point(94, 98);
            txtCity.Size = new System.Drawing.Size(270, 20);
            txtCity.TabIndex = 9;
            Controls.Add(txtCity);
    
            // Label: County
            lblCounty = new Label();
            lblCounty.AutoSize = true;
            lblCounty.Location = new System.Drawing.Point(13, 127);
            lblCounty.TabIndex = 10;
            lblCounty.Text = "County:";
            Controls.Add(lblCounty);
    
            // Text Box: County
            txtCounty = new TextBox();
            txtCounty.Location = new System.Drawing.Point(94, 124);
            txtCounty.Size = new System.Drawing.Size(88, 20);
            txtCounty.TabIndex = 11;
            Controls.Add(txtCounty);
    
            // Label: State
            lblState = new Label();
            lblState.AutoSize = true;
            lblState.Location = new System.Drawing.Point(195, 127);
            lblState.TabIndex = 12;
            lblState.Text = "State:";
            Controls.Add(lblState);
    
            // Text Box: State
            txtState = new TextBox();
            txtState.Location = new System.Drawing.Point(276, 124);
            txtState.Size = new System.Drawing.Size(88, 20);
            txtState.TabIndex = 13;
            Controls.Add(txtState);
           
            // Label: ZIP Code
            lblZIPCode = new Label();
            lblZIPCode.AutoSize = true;
            lblZIPCode.Location = new System.Drawing.Point(13, 153);
            lblZIPCode.TabIndex = 14;
            lblZIPCode.Text = "ZIP Code:";
            Controls.Add(lblZIPCode);
    
            // Text Box: ZIP Code
            txtZIPCode = new TextBox();
            txtZIPCode.Location = new System.Drawing.Point(94, 150);
            txtZIPCode.Size = new System.Drawing.Size(88, 20);
            txtZIPCode.TabIndex = 15;
            Controls.Add(txtZIPCode);
    
            // Label: Marital Status
            lblMaritalStatus = new Label();
            lblMaritalStatus.AutoSize = true;
            lblMaritalStatus.Location = new System.Drawing.Point(195, 153);
            lblMaritalStatus.TabIndex = 16;
            lblMaritalStatus.Text = "Marital Status:";
            Controls.Add(lblMaritalStatus);
    
            // Combo Box: Maritals Status
            cbxMaritalsStatus = new ComboBox();
            cbxMaritalsStatus.Items.AddRange(new object[] { "Single", "Married"});
            cbxMaritalsStatus.Location = new System.Drawing.Point(276, 150);
            cbxMaritalsStatus.Size = new System.Drawing.Size(88, 21);
            cbxMaritalsStatus.TabIndex = 17;
            Controls.Add(cbxMaritalsStatus);
    
            // Label: Exemptions
            lblExemptions = new Label();
            lblExemptions.AutoSize = true;
            lblExemptions.Location = new System.Drawing.Point(13, 179);
            lblExemptions.TabIndex = 18;
            lblExemptions.Text = "Exemptions:";
            Controls.Add(lblExemptions);
    
            // Text Box: Exemptions
            txtExemptions = new TextBox();
            txtExemptions.Location = new System.Drawing.Point(94, 176);
            txtExemptions.Size = new System.Drawing.Size(88, 20);
            txtExemptions.TabIndex = 19;
            txtExemptions.Text = "0";
            txtExemptions.TextAlign = HorizontalAlignment.Right;
            Controls.Add(txtExemptions);
    
            // Label: Hourly Salary
            lblHourlySalary = new Label();
            lblHourlySalary.AutoSize = true;
            lblHourlySalary.Location = new System.Drawing.Point(195, 180);
            lblHourlySalary.TabIndex = 20;
            lblHourlySalary.Text = "Hourly Salary:";
            Controls.Add(lblHourlySalary);
            
            // Text Box: Hourly Salary
            txtHourlySalary = new TextBox();
            txtHourlySalary.Location = new System.Drawing.Point(276, 177);
            txtHourlySalary.Size = new System.Drawing.Size(88, 20);
            txtHourlySalary.TabIndex = 21;
            Controls.Add(txtHourlySalary);
    
            // Button: OK
            btnOK = new Button();
            btnOK.DialogResult = DialogResult.OK;
            btnOK.Location = new System.Drawing.Point(202, 220);
            btnOK.Size = new System.Drawing.Size(75, 23);
            btnOK.TabIndex = 22;
            btnOK.Text = "OK";
            Controls.Add(btnOK);
    
            // Button: Cancel
            btnCancel = new Button();
            btnCancel.DialogResult = DialogResult.Cancel;
            btnCancel.Location = new System.Drawing.Point(288, 220);
            btnCancel.Size = new System.Drawing.Size(75, 23);
            btnCancel.TabIndex = 23;
            btnCancel.Text = "Cancel";
            Controls.Add(btnCancel);
    
            // Form: New Employee
            AcceptButton = this.btnOK;
            CancelButton = this.btnCancel;
            ClientSize = new System.Drawing.Size(388, 260);
            FormBorderStyle = FormBorderStyle.FixedDialog;
            MaximizeBox = false;
            MinimizeBox = false;
            StartPosition = FormStartPosition.CenterScreen;
            Text = "Fun Department Store - New Employee";
        }
    }
  2. To save and start a new file, on the main menu, click File -> New
  3. When asked whether you want to save, click Save
  4. In the top combo box, select and display the C: drive
  5. Click the New Folder button
  6. Type FunDS1 and press Enter twice to display it in the top combo box
  7. Change the Save As Type combo box to All Files
  8. Set the File Name to EmployeeNew.cs
  9. Click Save
  10. In the empty document, type the following:
    using System;
    using System.IO;
    using System.Xml;
    using System.Drawing;
    using System.Windows.Forms;
    
    public class EmployeeEditor : Form
    {
        private Label lblEmployeeNumber;
        public TextBox txtEmployeeNumber;
        private Button btnFind;
        private Label lblFirstName;
        public TextBox txtFirstName;
        private Label lblLastName;
        public TextBox txtLastName;
        private Label lblAddress;
        public TextBox txtAddress;
        private Label lblCity;
        public TextBox txtCity;
        private Label lblCounty;
        public TextBox txtCounty;
        private Label lblState;
        public TextBox txtState;
        private Label lblZIPCode;
        public TextBox txtZIPCode;
        private Label lblMaritalStatus;
        public ComboBox cbxMaritalsStatus;
        private Label lblExemptions;
        public TextBox txtExemptions;
        private Label lblHourlySalary;
        public TextBox txtHourlySalary;
        private Button btnSubmit;
        private Button btnClose;
    
        public EmployeeEditor()
        {
            InitializeComponent();
        }
    
        private void InitializeComponent()
        {
            // Label: Employee Number
            lblEmployeeNumber = new Label();
            lblEmployeeNumber.AutoSize = true;
            lblEmployeeNumber.Location = new System.Drawing.Point(13, 18);
            lblEmployeeNumber.TabIndex = 0;
            lblEmployeeNumber.Text = "Employee #:";
            Controls.Add(lblEmployeeNumber);
    
            // Text Box: Employee Number
            txtEmployeeNumber = new TextBox();
            txtEmployeeNumber.Location = new System.Drawing.Point(94, 16);
            txtEmployeeNumber.Size = new System.Drawing.Size(88, 20);
            txtEmployeeNumber.TabIndex = 1;
            Controls.Add(txtEmployeeNumber);
    
            // Button: Find
            btnFind = new Button();
            btnFind.Location = new System.Drawing.Point(201, 18);
            btnFind.Size = new System.Drawing.Size(75, 23);
            btnFind.TabIndex = 2;
            btnFind.Text = "Find";
            btnFind.Click += new System.EventHandler(btnFindClick);
            Controls.Add(btnFind);
    
            // Label: First Name
            lblFirstName = new Label();
            lblFirstName.AutoSize = true;
            lblFirstName.Location = new System.Drawing.Point(13, 49);
            lblFirstName.TabIndex = 5;
            lblFirstName.Text = "First Name:";
            Controls.Add(lblFirstName);
    
            // Text Box: First Name
            txtFirstName = new TextBox();
            txtFirstName.Location = new System.Drawing.Point(94, 46);
            txtFirstName.Size = new System.Drawing.Size(88, 20);
            txtFirstName.TabIndex = 6;
            Controls.Add(txtFirstName);
    
            // Label: Last Name
            lblLastName = new Label();
            lblLastName.AutoSize = true;
            lblLastName.Location = new System.Drawing.Point(195, 49);
            lblLastName.TabIndex = 7;
            lblLastName.Text = "Last Name:";
            Controls.Add(lblLastName);
    
            // Text Box: Last Name
            txtLastName = new TextBox();
            txtLastName.Location = new System.Drawing.Point(276, 46);
            txtLastName.Size = new System.Drawing.Size(88, 20);
            txtLastName.TabIndex = 8;
            Controls.Add(txtLastName);
    
            // Label: Address
            lblAddress = new Label();
            lblAddress.AutoSize = true;
            lblAddress.Location = new System.Drawing.Point(13, 75);
            lblAddress.TabIndex = 9;
            lblAddress.Text = "Address:";
            Controls.Add(lblAddress);
    
            // Text Box: Address
            txtAddress = new TextBox();
            txtAddress.Location = new System.Drawing.Point(94, 72);
            txtAddress.Size = new System.Drawing.Size(270, 20);
            txtAddress.TabIndex = 10;
            Controls.Add(txtAddress);
    
            // Label: City
            lblCity = new Label();
            lblCity.AutoSize = true;
            lblCity.Location = new System.Drawing.Point(13, 101);
            lblCity.TabIndex = 11;
            lblCity.Text = "City:";
            Controls.Add(lblCity);
    
            // Text Box: City
            txtCity = new TextBox();
            txtCity.Location = new System.Drawing.Point(94, 98);
            txtCity.Size = new System.Drawing.Size(270, 20);
            txtCity.TabIndex = 12;
            Controls.Add(txtCity);
    
            // Label: County
            lblCounty = new Label();
            lblCounty.AutoSize = true;
            lblCounty.Location = new System.Drawing.Point(13, 127);
            lblCounty.TabIndex = 13;
            lblCounty.Text = "County:";
            Controls.Add(lblCounty);
    
            // Text Box: County
            txtCounty = new TextBox();
            txtCounty.Location = new System.Drawing.Point(94, 124);
            txtCounty.Size = new System.Drawing.Size(88, 20);
            txtCounty.TabIndex = 14;
            Controls.Add(txtCounty);
    
            // Label: State
            lblState = new Label();
            lblState.AutoSize = true;
            lblState.Location = new System.Drawing.Point(195, 127);
            lblState.TabIndex = 15;
            lblState.Text = "State:";
            Controls.Add(lblState);
    
            // Text Box: State
            txtState = new TextBox();
            txtState.Location = new System.Drawing.Point(276, 124);
            txtState.Size = new System.Drawing.Size(88, 20);
            txtState.TabIndex = 16;
            Controls.Add(txtState);
    
            // Label: ZIP Code
            lblZIPCode = new Label();
            lblZIPCode.AutoSize = true;
            lblZIPCode.Location = new System.Drawing.Point(13, 153);
            lblZIPCode.TabIndex = 17;
            lblZIPCode.Text = "ZIP Code:";
            Controls.Add(lblZIPCode);
    
            // Text Box: ZIP Code
            txtZIPCode = new TextBox();
            txtZIPCode.Location = new System.Drawing.Point(94, 150);
            txtZIPCode.Size = new System.Drawing.Size(88, 20);
            txtZIPCode.TabIndex = 18;
            Controls.Add(txtZIPCode);
    
            // Label: Marital Status
            lblMaritalStatus = new Label();
            lblMaritalStatus.AutoSize = true;
            lblMaritalStatus.Location = new System.Drawing.Point(195, 153);
            lblMaritalStatus.TabIndex = 19;
            lblMaritalStatus.Text = "Marital Status:";
            Controls.Add(lblMaritalStatus);
            
            // Combo Box: MaritalsStatus
            cbxMaritalsStatus = new ComboBox();
            cbxMaritalsStatus.Items.AddRange(new object[] { "Single", "Married"});
            cbxMaritalsStatus.Location = new System.Drawing.Point(276, 150);
            cbxMaritalsStatus.Size = new System.Drawing.Size(88, 21);
            cbxMaritalsStatus.TabIndex = 20;
            Controls.Add(cbxMaritalsStatus);
    
            // Label: Exemptions
            lblExemptions = new Label();
            lblExemptions.AutoSize = true;
            lblExemptions.Location = new System.Drawing.Point(13, 179);
            lblExemptions.TabIndex = 21;
            lblExemptions.Text = "Exemptions:";
            Controls.Add(lblExemptions);
    
            // Text Box: Exemptions
            txtExemptions = new TextBox();
            txtExemptions.Location = new System.Drawing.Point(94, 176);
            txtExemptions.Size = new System.Drawing.Size(88, 20);
            txtExemptions.TabIndex = 22;
            txtExemptions.Text = "0";
            txtExemptions.TextAlign = HorizontalAlignment.Right;
            Controls.Add(txtExemptions);
    
            // Label: Hourly Salary
            lblHourlySalary = new Label();
            lblHourlySalary.AutoSize = true;
            lblHourlySalary.Location = new System.Drawing.Point(195, 180);
            lblHourlySalary.TabIndex = 23;
            lblHourlySalary.Text = "Hourly Salary:";
            Controls.Add(lblHourlySalary);
    
            // Text Box: Hourly Salary
            txtHourlySalary = new TextBox();
            txtHourlySalary.Location = new System.Drawing.Point(276, 177);
            txtHourlySalary.Size = new System.Drawing.Size(88, 20);
            txtHourlySalary.TabIndex = 24;
            Controls.Add(txtHourlySalary);
    
            // Button: Submit
            btnSubmit = new Button();
            btnSubmit.Location = new System.Drawing.Point(207, 218);
            btnSubmit.Size = new System.Drawing.Size(84, 23);
            btnSubmit.TabIndex = 3;
            btnSubmit.Text = "Submit";
            btnSubmit.Click += new System.EventHandler(btnSubmitClick);
            Controls.Add(btnSubmit);
    
            // Button: Close
            btnClose = new Button();
            btnClose.Location = new System.Drawing.Point(297, 218);
            btnClose.Size = new System.Drawing.Size(75, 23);
            btnClose.TabIndex = 4;
            btnClose.Text = "Close";
            btnClose.Click += new System.EventHandler(btnCloseClick);
            Controls.Add(btnClose);
                
            // Form: Employee Editor
            ClientSize = new System.Drawing.Size(394, 259);
            FormBorderStyle = FormBorderStyle.FixedDialog;
            MaximizeBox = false;
            MinimizeBox = false;
            StartPosition = FormStartPosition.CenterScreen;
            Text = "Fun Department Store: Employee Editor";
        }
    
        private void btnFindClick(object sender, EventArgs e)
        {
            bool employeeFound = false;
            XmlDocument xdEmployees = new XmlDocument();
            string strEmployeesFile = @"C:\Fun Department Store\Employees.xml";
    
            if (string.IsNullOrEmpty(txtEmployeeNumber.Text))
            {
                MessageBox.Show("You must enter an employee number.", "FunDS - Employees Records", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
    
            if (File.Exists(strEmployeesFile))
            {
                using (FileStream fsEmployees = new FileStream(strEmployeesFile, FileMode.Open, FileAccess.Read))
                {
                    xdEmployees.Load(fsEmployees);
    
                    XmlNodeList xnlEmployees = xdEmployees.GetElementsByTagName("EmployeeNumber");
    
                    foreach (XmlNode xnEmployee in xnlEmployees)
                    {
                        if (xnEmployee.InnerText == txtEmployeeNumber.Text)
                        {
                            txtFirstName.Text = xnEmployee.NextSibling.InnerText; // First Name
                            txtLastName.Text = xnEmployee.NextSibling.NextSibling.InnerText; // Last Name
                            txtAddress.Text = xnEmployee.NextSibling.NextSibling.NextSibling.InnerText; // Address
                            txtCity.Text = xnEmployee.NextSibling.NextSibling.NextSibling.NextSibling.InnerText; // City
                            txtCounty.Text = xnEmployee.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText; // County
                            txtState.Text = xnEmployee.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText; // State
                            txtZIPCode.Text = xnEmployee.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText; // ZIP Code
                            cbxMaritalsStatus.Text = xnEmployee.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText; // Marital Status
                            txtExemptions.Text = xnEmployee.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText; // Exemptions
                            txtHourlySalary.Text = xnEmployee.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText; // Hourly Salary
    
                            employeeFound = true;
                        }
                    }
    
                    if (employeeFound == false)
                    {
                        MessageBox.Show("There is no staff member with that employee number.", "FunDS - Employees Records", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }
            }
        }
    
        private void btnSubmitClick(object sender, EventArgs e)
        {
            XmlDocument xdEmployees = new XmlDocument();
            string strEmployeesFile = @"C:\Fun Department Store\Employees.xml";
    
            if (string.IsNullOrEmpty(txtEmployeeNumber.Text))
            {
                MessageBox.Show("You must enter an employee number.",
                                "FunDS - Employees Records",
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
    
            if (File.Exists(strEmployeesFile))
            {
                using (FileStream fsEmployees = new FileStream(strEmployeesFile, FileMode.Open, FileAccess.Read))
                {
                    xdEmployees.Load(fsEmployees);
    
                    XmlNodeList xnlEmployees = xdEmployees.GetElementsByTagName("EmployeeNumber");
    
                    foreach (XmlNode xnEmployee in xnlEmployees)
                    {
                        if (xnEmployee.InnerText == txtEmployeeNumber.Text)
                        {
                            xnEmployee.ParentNode.InnerXml = "<EmployeeNumber>" + txtEmployeeNumber.Text + "</EmployeeNumber>" +
                                                             "<FirstName>" + txtFirstName.Text + "</FirstName>" +
                                                             "<LastName>" + txtLastName.Text + "</LastName>" +
                                                             "<Address>" + txtAddress.Text + "</Address>" +
                                                             "<City>" + txtCity.Text + "</City>" +
                                                             "<County>" + txtCounty.Text + "</County>" +
                                                             "<State>" + txtState.Text + "</State>" +
                                                             "<ZIPCode>" + txtZIPCode.Text + "</ZIPCode>" +
                                                             "<MaritalStatus>" + cbxMaritalsStatus.Text + "</MaritalStatus>" +
                                                             "<Exemptions>" + txtExemptions.Text + "</Exemptions>" +
                                                             "<HourlySalary>" + txtHourlySalary.Text + "</HourlySalary>";
                            break;
                        }
                    }
                }
    
                using (FileStream fsEmployees = new FileStream(strEmployeesFile, FileMode.Create, FileAccess.Write))
                {
                    xdEmployees.Save(fsEmployees);
    
                    MessageBox.Show("The employee's record has been updated.",
                                    "FunDS - Employees Records",
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
    
            Close();
        }
    
        private void btnCloseClick(object sender, EventArgs e)
        {
            Close();
        }
    }
  11. To save and start a new filew, on the main menu, click File -> New
  12. When asked whether you want to save, click Save
  13. Make sure the FunDS1 folder is displaying in the top combo box. Set the Save As Type combo box to All Files
  14. Set the File Name to EmployeeEditor.cs
  15. Click Save
  16. In the empty document, type the following:
    using System;
    using System.IO;
    using System.Xml;
    using System.Drawing;
    using System.Windows.Forms;
    
    public class Employees : Form
    {
        private ColumnHeader colEmployeeNumber;
        private ColumnHeader colFirstName;
        private ColumnHeader colLastName;
        private ColumnHeader colAddress;
        private ColumnHeader colCity;
        private ColumnHeader colCounty;
        private ColumnHeader colState;
        private ColumnHeader colZIPCode;
        private ColumnHeader colMaritalStatus;
        private ColumnHeader colExemptions;
        private ColumnHeader colHourlySalary;
        private ListView lvwEmployees;
    
        private Label lblNumberOfEmployees;
        private TextBox txtNumberOfEmployees;
        private Button btnNewEmployee;
        private Button btnClose;
    
        public Employees()
        {
            InitializeComponent();
        }
    
        private void InitializeComponent()
        {
            // Column: Employee Number
            colEmployeeNumber = new ColumnHeader();
            colEmployeeNumber.Text = "Employee #";
            colEmployeeNumber.Width = 70;
    
            // Column: First Name
            colFirstName = new ColumnHeader();
            colFirstName.Text = "First Name";
            colFirstName.Width = 65;
    
            // Column: Last Name
            colLastName = new ColumnHeader();
            colLastName.Text = "Last Name";
            colLastName.Width = 70;
    
            // Column: Address
            colAddress = new ColumnHeader();
            colAddress.Text = "Address";
            colAddress.Width = 125;
    
            // Column: City
            colCity = new ColumnHeader();
            colCity.Text = "City";
            colCity.Width = 75;
    
            // Column: Username
            colCounty = new ColumnHeader();
            colCounty.Text = "County";
    
            // Column: State
            colState = new ColumnHeader();
            colState.Text = "State";
            colState.Width = 40;
    
            // Column: ZIP Code
            colZIPCode = new ColumnHeader();
            colZIPCode.Text = "ZIP Code";
    
            // Column: Marital Status
            colMaritalStatus = new ColumnHeader();
            colMaritalStatus.Text = "Marital Status";
            colMaritalStatus.Width = 80;
    
            // Column: Exemptions
            colExemptions = new ColumnHeader();
            colExemptions.Text = "Exemptions";
            colExemptions.TextAlign = HorizontalAlignment.Right;
            colExemptions.Width = 70;
    
            // Column: Hourly Salary
            colHourlySalary = new ColumnHeader();
            colHourlySalary.Text = "Salary";
            colHourlySalary.TextAlign = HorizontalAlignment.Right;
            colHourlySalary.Width = 45;
    
            // List View: Employees
            lvwEmployees = new ListView();
            lvwEmployees.Anchor = AnchorStyles.Top | AnchorStyles.Bottom
                                | AnchorStyles.Left | AnchorStyles.Right;
            lvwEmployees.Columns.AddRange(new ColumnHeader[]
            {
                colEmployeeNumber, colFirstName, colLastName,
                colAddress, colCity, colCounty, colState, colZIPCode, 
                colMaritalStatus, colExemptions, colHourlySalary
            });
            lvwEmployees.FullRowSelect = true;
            lvwEmployees.GridLines = true;
            lvwEmployees.Location = new System.Drawing.Point(16, 12);
            lvwEmployees.Size = new System.Drawing.Size(785, 125);
            lvwEmployees.TabIndex = 25;
            lvwEmployees.View = View.Details;
            lvwEmployees.DoubleClick += new EventHandler(lvwEmployeesDoubleClick);
    
            // Label: NumberOfEmployees
            lblNumberOfEmployees = new Label();
            lblNumberOfEmployees.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
            lblNumberOfEmployees.AutoSize = true;
            lblNumberOfEmployees.Location = new Point(12, 158);
            lblNumberOfEmployees.Size = new System.Drawing.Size(113, 13);
            lblNumberOfEmployees.TabIndex = 28;
            lblNumberOfEmployees.Text = "Number of Employees:";
    
            // Text Box: NumberOfEmployees
            txtNumberOfEmployees = new TextBox();
            txtNumberOfEmployees.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
            txtNumberOfEmployees.Location = new Point(131, 155);
            txtNumberOfEmployees.Size = new System.Drawing.Size(60, 20);
            txtNumberOfEmployees.TabIndex = 29;
            txtNumberOfEmployees.TextAlign = HorizontalAlignment.Right;
    
            // Button: NewEmployee
            btnNewEmployee = new Button();
            btnNewEmployee.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
            btnNewEmployee.Location = new Point(564, 153);
            btnNewEmployee.Size = new System.Drawing.Size(109, 23);
            btnNewEmployee.TabIndex = 27;
            btnNewEmployee.Text = "New Employee ...";
            btnNewEmployee.Click += new System.EventHandler(btnNewEmployeeClick);
    
            // Button: Close
            btnClose = new Button();
            btnClose.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
            btnClose.Location = new Point(679, 153);
            btnClose.Size = new System.Drawing.Size(75, 23);
            btnClose.TabIndex = 26;
            btnClose.Text = "Close";
            btnClose.Click += new System.EventHandler(btnCloseClick);
    
            // Form: Employees
            ClientSize = new System.Drawing.Size(820, 185);
            Controls.Add(lvwEmployees);
            Controls.Add(txtNumberOfEmployees);
            Controls.Add(lblNumberOfEmployees);
            Controls.Add(btnNewEmployee);
            Controls.Add(btnClose);
    
            MaximizeBox = false;
            StartPosition = FormStartPosition.CenterScreen;
            Text = "FunDS - Employees";
            Load += new System.EventHandler(EmployeesLoad);
        }
    
        private void ShowEmployees()
        {
            XmlDocument xdEmployees = new XmlDocument();
            string strEmployeesFile = @"C:\Fun Department Store\Employees.xml";
    
            if( File.Exists(strEmployeesFile))
            {
                lvwEmployees.Items.Clear();
                using (FileStream fsEmployees = new FileStream(strEmployeesFile, FileMode.Open, FileAccess.Read))
                {
                    xdEmployees.Load(fsEmployees);
    
                    XmlNodeList xnlEmployees = xdEmployees.GetElementsByTagName("EmployeeNumber");
    
                    foreach (XmlNode xnEmployee in xnlEmployees)
                    {
                        ListViewItem lviEmployee = new ListViewItem(xnEmployee.InnerText); // Employee Number
                        lviEmployee.SubItems.Add(xnEmployee.NextSibling.InnerText); // First Name
                        lviEmployee.SubItems.Add(xnEmployee.NextSibling.NextSibling.InnerText); // Last Name
                        lviEmployee.SubItems.Add(xnEmployee.NextSibling.NextSibling.NextSibling.InnerText); // Address
                        lviEmployee.SubItems.Add(xnEmployee.NextSibling.NextSibling.NextSibling.NextSibling.InnerText); // City
                        lviEmployee.SubItems.Add(xnEmployee.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText); // County
                        lviEmployee.SubItems.Add(xnEmployee.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText); // State
                        lviEmployee.SubItems.Add(xnEmployee.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText); // ZIP Code
                        lviEmployee.SubItems.Add(xnEmployee.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText); // Marital Status
                        lviEmployee.SubItems.Add(xnEmployee.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText); // Exemptions
                        lviEmployee.SubItems.Add(xnEmployee.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText); // Hourly Salary
    
                        lvwEmployees.Items.Add(lviEmployee);
                    }
    
                    txtNumberOfEmployees.Text = xnlEmployees.Count.ToString();
                }
            }
        }
    
        private void EmployeesLoad(object sender, EventArgs e)
        {
            ShowEmployees();
        }
    
        private void btnNewEmployeeClick(object sender, EventArgs e)
        {
            string strEmployeeNumber, strFirstName, strLastName, strCounty,
                   strAddress, strCity, strState, strZIPCode, strMaritalStatus;
            int iExemptions;
            double dblHourlySalary;
    
            EmployeeNew en = new EmployeeNew();
            Random rndEmployeeNumber = new Random();
            XmlDocument xdEmployees = new XmlDocument();
            string strEmployeesFile = @"C:\Fun Department Store\Employees.xml";
    
            en.txtEmployeeNumber.Text = rndEmployeeNumber.Next(100000, 999999).ToString();
    
            if (en.ShowDialog() == DialogResult.OK)
            {
                strEmployeeNumber = en.txtEmployeeNumber.Text;
                strFirstName = en.txtFirstName.Text;
                strLastName = en.txtLastName.Text;
                strMaritalStatus = en.cbxMaritalsStatus.Text;
                iExemptions = int.Parse(en.txtExemptions.Text);
                strAddress = en.txtAddress.Text;
                strCity = en.txtCity.Text;
                strCounty = en.txtCounty.Text;
                strState = en.txtState.Text;
                strZIPCode = en.txtZIPCode.Text;
                dblHourlySalary = double.Parse(en.txtHourlySalary.Text);
    
                if (!File.Exists(strEmployeesFile))
                {
                    xdEmployees.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                                        "<Employees></Employees>");
                    xdEmployees.Save(strEmployeesFile);
                }
    
                xdEmployees.Load(strEmployeesFile);
    
                XmlElement elmXML = xdEmployees.CreateElement("Employee");
                // Create the XML code of the child element of Employee
                string strNewEmployee = "<EmployeeNumber>" + strEmployeeNumber + "</EmployeeNumber>" +
                                        "<FirstName>" + strFirstName + "</FirstName>" +
                                        "<LastName>" + strLastName + "</LastName>" +
                                        "<Address>" + strAddress + "</Address>" +
                                        "<City>" + strCity + "</City>" +
                                        "<County>" + strCounty + "</County>" +
                                        "<State>" + strState + "</State>" +
                                        "<ZIPCode>" + strZIPCode + "</ZIPCode>" +
                                        "<MaritalStatus>" + strMaritalStatus + "</MaritalStatus>" +
                                        "<Exemptions>" + iExemptions + "</Exemptions>" +
                                        "<HourlySalary>" + dblHourlySalary + "</HourlySalary>";
                elmXML.InnerXml = strNewEmployee;
                // Append the new element as a child of employees
                xdEmployees.DocumentElement.AppendChild(elmXML);
    
                // Save the XML file
                xdEmployees.Save(strEmployeesFile);
    
                ShowEmployees();
            }
        }
    
        private void lvwEmployeesDoubleClick(object sender, EventArgs e)
        {
            EmployeeEditor ee = new EmployeeEditor();
            XmlDocument xdEmployees = new XmlDocument();
            string strEmployeesFile = @"C:\Fun Department Store\Employees.xml";
    
            if (lvwEmployees.Items.Count <= 0)
                return;
    
            using (FileStream fsEmployees = new FileStream(strEmployeesFile, FileMode.Open, FileAccess.Read))
            {
                xdEmployees.Load(fsEmployees);
    
                XmlNodeList xnlEmployees = xdEmployees.GetElementsByTagName("EmployeeNumber");
    
                foreach (XmlNode xnEmployee in xnlEmployees)
                {
                    if (xnEmployee.InnerText == lvwEmployees.SelectedItems[0].Text)
                    {
                        ee.txtEmployeeNumber.Text = xnEmployee.InnerText; // 
                        ee.txtFirstName.Text = xnEmployee.NextSibling.InnerText; // First Name
                        ee.txtLastName.Text = xnEmployee.NextSibling.NextSibling.InnerText; // Last Name
                        ee.txtAddress.Text = xnEmployee.NextSibling.NextSibling.NextSibling.InnerText; // Address
                        ee.txtCity.Text = xnEmployee.NextSibling.NextSibling.NextSibling.NextSibling.InnerText; // City
                        ee.txtCounty.Text = xnEmployee.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText; // County
                        ee.txtState.Text = xnEmployee.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText; // State
                        ee.txtZIPCode.Text = xnEmployee.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText; // ZIP Code
                        ee.cbxMaritalsStatus.Text = xnEmployee.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText; // Marital Status
                        ee.txtExemptions.Text = xnEmployee.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText; // Exemptions
                        ee.txtHourlySalary.Text = xnEmployee.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText; // Hourly Salary
                    }
                }
            }
    
            ee.ShowDialog();
            ShowEmployees();
        }
    
        private void btnCloseClick(object sender, EventArgs e)
        {
            Close();
        }
    }
  17. To save, on the main menu, click File -> New
  18. When asked whether you want to save, click Save
  19. Make sure the FunDS1 folder is displaying in the top combo box and change the Save As Type to All Files
  20. Set the File Name to Employees.cs
  21. Click Save

Handling Store Items

Items (such as clothing, jewelry, and accessories, etc) are the basis of the business. For our database, the three most important pieces of information about each item are the item number (which must be unique for each item), a name or short description, and a price. Other pieces of information we will consider for some items are the manufacturer, the category, and the subcategory. We will also apply a discount rate to some items.

Practical LearningPractical Learning: Creating Store Items

  1. In the empty document, type the following:
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    public class Manufacturer : Form
    {
        private Label lblManufacturer;
        public TextBox txtManufacturer;
        private Button btnOK;
        private Button btnCancel;
    
        public Manufacturer()
        {
            InitializeComponent();
        }
    
        private void InitializeComponent()
        {
            // Label: Manufacturer
            lblManufacturer = new Label();
            lblManufacturer.AutoSize = true;
            lblManufacturer.Location = new System.Drawing.Point(21, 20);
            lblManufacturer.TabIndex = 0;
            lblManufacturer.Text = "Manufacturer:";
            Controls.Add(lblManufacturer);
    
            // Text Box: Manufacturer
            txtManufacturer = new TextBox();
            txtManufacturer.Location = new System.Drawing.Point(100, 17);
            txtManufacturer.Size = new System.Drawing.Size(156, 20);
            txtManufacturer.TabIndex = 1;
            Controls.Add(txtManufacturer);
    
            // Button: OK
            btnOK = new Button();
            btnOK.DialogResult = DialogResult.OK;
            btnOK.Location = new System.Drawing.Point(100, 53);
            btnOK.Size = new System.Drawing.Size(75, 23);
            btnOK.TabIndex = 3;
            btnOK.Text = "OK";
            Controls.Add(btnOK);
    
            // Button: Cancel
            btnCancel = new Button();
            btnCancel.DialogResult = DialogResult.Cancel;
            btnCancel.Location = new System.Drawing.Point(181, 53);
            btnCancel.Size = new System.Drawing.Size(75, 23);
            btnCancel.TabIndex = 2;
            btnCancel.Text = "Cancel";
            Controls.Add(btnCancel);
    
            // Dialog Box: Manufacturer
            AcceptButton = this.btnOK;
            CancelButton = this.btnCancel;
            ClientSize = new System.Drawing.Size(276, 92);
            FormBorderStyle = FormBorderStyle.FixedDialog;
            MaximizeBox = false;
            MinimizeBox = false;
            ShowInTaskbar = false;
            Text = "FunDS - Manufacturer";
            StartPosition = FormStartPosition.CenterScreen;
        }
    }
  2. To save and start a new file, on the main menu, click File -> New
  3. When asked whether you want save, click Yes (or Save)
  4. In the top combo box, make sure FunDS1 is selected. Set the name to Manufacturer.cs
  5. Change the Save As Type to All Files
  6. Click Save
  7. In the empty document, type the following:
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    public class Category : Form
    {
        private Button btnOK;
        private Button btnCancel;
        private Label lblCategory;
        public TextBox txtCategory;
    
        public Category()
        {
            InitializeComponent();
        }
    
        private void InitializeComponent()
        {
            // Label: Category
            lblCategory = new Label();
            lblCategory.AutoSize = true;
            lblCategory.Location = new System.Drawing.Point(21, 20);
            lblCategory.TabIndex = 4;
            lblCategory.Text = "Category:";
            Controls.Add(lblCategory);
    
            // Text Box: Category
            txtCategory = new TextBox();
            txtCategory.Location = new System.Drawing.Point(100, 17);
            txtCategory.Size = new System.Drawing.Size(156, 20);
            txtCategory.TabIndex = 5;
            Controls.Add(txtCategory);
    
            // Button: OK
            btnOK = new Button();
            btnOK.DialogResult = DialogResult.OK;
            btnOK.Location = new System.Drawing.Point(100, 53);
            btnOK.Size = new System.Drawing.Size(75, 23);
            btnOK.TabIndex = 7;
            btnOK.Text = "OK";
            btnOK.UseVisualStyleBackColor = true;
            Controls.Add(btnOK);
    
            // Button: Cancel
            btnCancel = new Button();
            btnCancel.DialogResult = DialogResult.Cancel;
            btnCancel.Location = new System.Drawing.Point(181, 53);
            btnCancel.Size = new System.Drawing.Size(75, 23);
            btnCancel.TabIndex = 6;
            btnCancel.Text = "Cancel";
            Controls.Add(btnCancel);
    
            // Dialog Box: Category 
            AcceptButton = this.btnOK;
            CancelButton = this.btnCancel;
            ClientSize = new System.Drawing.Size(276, 93);
            FormBorderStyle = FormBorderStyle.FixedDialog;
            MaximizeBox = false;
            MinimizeBox = false;
            ShowInTaskbar = false;
            StartPosition = FormStartPosition.CenterScreen;
            Text = "FunDS - Category";
        }
    }
  8. To save and start a new file, on the main menu, click File -> New
  9. When asked whether you want to save, click Save
  10. Make sure FunDS1 is selected in the top combo box and set the File Name to Category.cs
  11. Set the Save As Type combo box to All Files
  12. Click Save
  13. In the empty document, type the following:
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    public class SubCategory : Form
    {
        private Label lblSubCategory;
        public TextBox txtSubCategory;
        private Button btnOK;
        private Button btnCancel;
    
        public SubCategory()
        {
            InitializeComponent();
        }
    
        private void InitializeComponent()
        {
            lblSubCategory = new Label();
            lblSubCategory.AutoSize = true;
            lblSubCategory.Location = new System.Drawing.Point(21, 20);
            lblSubCategory.TabIndex = 0;
            lblSubCategory.Text = "Sub-Category:";
            Controls.Add(lblSubCategory);
    
            txtSubCategory = new TextBox();
            txtSubCategory.Location = new System.Drawing.Point(100, 17);
            txtSubCategory.Size = new System.Drawing.Size(156, 20);
            txtSubCategory.TabIndex = 1;
            Controls.Add(txtSubCategory);
    
            btnOK = new Button();
            btnOK.DialogResult = DialogResult.OK;
            btnOK.Location = new System.Drawing.Point(100, 53);
            btnOK.Size = new System.Drawing.Size(75, 23);
            btnOK.TabIndex = 2;
            btnOK.Text = "OK";
            Controls.Add(btnOK);
    
            btnCancel = new Button();
            btnCancel.DialogResult = DialogResult.Cancel;
            btnCancel.Location = new System.Drawing.Point(181, 53);
            btnCancel.Size = new System.Drawing.Size(75, 23);
            btnCancel.TabIndex = 3;
            btnCancel.Text = "Cancel";
            Controls.Add(btnCancel);
    
            AcceptButton = btnOK;
            CancelButton = btnCancel;
            ClientSize = new System.Drawing.Size(276, 93);
            FormBorderStyle = FormBorderStyle.FixedDialog;
            MaximizeBox = false;
            MinimizeBox = false;
            ShowInTaskbar = false;
            StartPosition = FormStartPosition.CenterScreen;
            Text = "FunDS - Category";
        }
    }
  14. To save and start a new file, on the main menu, click File -> New
  15. When asked whether you want to save, click Save
  16. In the top combo box, make sure the FunDS1 folder is selected. Set the Save As Type combo box to All Files
  17. Set the File Name to SubCategory.cs
  18. Click Save
  19. In the empty document, type the following:
    using System;
    using System.IO;
    using System.Xml;
    using System.Drawing;
    using System.Windows.Forms;
    
    public class StoreItemNew : Form
    {
        private Label lblItemNumber;
        public TextBox txtItemNumber;
        private Label lblManufacturer;
        public ComboBox cbxManufacturers;
        private Button btnNewManufacturer;
        private Label lblCategory;
        private Label lblSubCategory;
        private Label lblUnitPrice;
        private Label lblDiscountRate;
        private Label lblDiscountPercent;
        private Button btnNewCategory;
        private Button btnNewSubCategory;
        private Button btnSubmit;
        private Button btnClose;
        public ComboBox cbxCategories;
        public ComboBox cbxSubCategories;
        private Label lblItemName;
        public TextBox txtItemName;
        private Label lblItemSize;
        public TextBox txtItemSize;
        public TextBox txtUnitPrice;
        public TextBox txtDiscountRate;
        private Button btnReset;
    
        public StoreItemNew()
        {
            InitializeComponent();
        }
    
        private void InitializeComponent()
        {
            lblItemNumber = new Label();
            lblItemNumber.AutoSize = true;
            lblItemNumber.Location = new Point(17, 24);
            lblItemNumber.TabIndex = 2;
            lblItemNumber.Text = "Item Number:";
            Controls.Add(lblItemNumber);
    
            // Label: Item Number
            txtItemNumber = new TextBox();
            txtItemNumber.Location = new Point(96, 21);
            txtItemNumber.Size = new System.Drawing.Size(94, 20);
            txtItemNumber.TabIndex = 3;
            Controls.Add(txtItemNumber);
    
            // Label: Manufacturer
            lblManufacturer = new Label();
            lblManufacturer.AutoSize = true;
            lblManufacturer.Location = new Point(17, 54);
            lblManufacturer.TabIndex = 6;
            lblManufacturer.Text = "Manufacturer:";
            Controls.Add(lblManufacturer);
    
            // Combo Box: Manufacturers
            cbxManufacturers = new ComboBox();
            cbxManufacturers.DropDownStyle = ComboBoxStyle.DropDownList;
            cbxManufacturers.FormattingEnabled = true;
            cbxManufacturers.Location = new Point(96, 50);
            cbxManufacturers.Size = new System.Drawing.Size(200, 21);
            cbxManufacturers.TabIndex = 7;
            Controls.Add(cbxManufacturers);
    
            // Button: New Manufacturer
            btnNewManufacturer = new Button();
            btnNewManufacturer.Location = new Point(302, 50);
            btnNewManufacturer.Size = new System.Drawing.Size(50, 23);
            btnNewManufacturer.TabIndex = 21;
            btnNewManufacturer.Text = "New...";
            btnNewManufacturer.Click += new System.EventHandler(btnNewManufacturerClick);
            Controls.Add(btnNewManufacturer);
    
            // Label: Category
            lblCategory = new Label();
            lblCategory.AutoSize = true;
            lblCategory.Location = new Point(17, 82);
            lblCategory.TabIndex = 8;
            lblCategory.Text = "Category:";
            Controls.Add(lblCategory);
    
            // Combo Box: Categories
            cbxCategories = new ComboBox();
            cbxCategories.DropDownStyle = ComboBoxStyle.DropDownList;
            cbxCategories.FormattingEnabled = true;
            cbxCategories.Location = new Point(96, 79);
            cbxCategories.Size = new System.Drawing.Size(200, 21);
            cbxCategories.TabIndex = 9;
            Controls.Add(cbxCategories);
    
            // Button: New Category
            btnNewCategory = new Button();
            btnNewCategory.Location = new Point(302, 79);
            btnNewCategory.Size = new System.Drawing.Size(50, 23);
            btnNewCategory.TabIndex = 22;
            btnNewCategory.Text = "New...";
            btnNewCategory.Click += new System.EventHandler(btnNewCategoryClick);
            Controls.Add(btnNewCategory);
    
            // Label: Sub-Category
            lblSubCategory = new Label();
            lblSubCategory.AutoSize = true;
            lblSubCategory.Location = new Point(17, 113);
            lblSubCategory.TabIndex = 10;
            lblSubCategory.Text = "Sub-Category:";
            Controls.Add(lblSubCategory);
    
            // Combo Box: Sub-Categories
            cbxSubCategories = new ComboBox();
            cbxSubCategories.DropDownStyle = ComboBoxStyle.DropDownList;
            cbxSubCategories.FormattingEnabled = true;
            cbxSubCategories.Location = new Point(96, 109);
            cbxSubCategories.Size = new System.Drawing.Size(200, 21);
            cbxSubCategories.TabIndex = 11;
            Controls.Add(cbxSubCategories);
    
            // Button: New Sub-Category
            btnNewSubCategory = new Button();
            btnNewSubCategory.Location = new Point(302, 109);
            btnNewSubCategory.Size = new System.Drawing.Size(50, 23);
            btnNewSubCategory.TabIndex = 23;
            btnNewSubCategory.Text = "New...";
            btnNewSubCategory.Click += new System.EventHandler(btnNewSubCategoryClick);
            Controls.Add(btnNewSubCategory);
    
            // Label: Item Name
            lblItemName = new Label();
            lblItemName.AutoSize = true;
            lblItemName.Location = new Point(17, 142);
            lblItemName.TabIndex = 12;
            lblItemName.Text = "Item Name:";
            Controls.Add(lblItemName);
    
            // Text Box: Item Name
            txtItemName = new TextBox();
            txtItemName.Location = new Point(96, 139);
            txtItemName.Size = new System.Drawing.Size(256, 20);
            txtItemName.TabIndex = 13;
            Controls.Add(txtItemName);
    
            // Label: Item Size
            lblItemSize = new Label();
            lblItemSize.AutoSize = true;
            lblItemSize.Location = new Point(17, 172);
            lblItemSize.TabIndex = 14;
            lblItemSize.Text = "Item Size:";
            Controls.Add(lblItemSize);
    
            // Text Box: Item Size
            txtItemSize = new TextBox();
            txtItemSize.Location = new Point(96, 169);
            txtItemSize.Size = new System.Drawing.Size(256, 20);
            txtItemSize.TabIndex = 15;
            Controls.Add(txtItemSize);
    
            // Label: Unit Price
            lblUnitPrice = new Label();
            lblUnitPrice.AutoSize = true;
            lblUnitPrice.Location = new Point(17, 201);
            lblUnitPrice.TabIndex = 16;
            lblUnitPrice.Text = "Unit Price:";
            Controls.Add(lblUnitPrice);
    
            // Text Box: Unit Price
            txtUnitPrice = new TextBox();
            txtUnitPrice.Location = new Point(96, 198);
            txtUnitPrice.Size = new System.Drawing.Size(94, 20);
            txtUnitPrice.TabIndex = 17;
            txtUnitPrice.Text = "0.00";
            txtUnitPrice.TextAlign = HorizontalAlignment.Right;
            Controls.Add(txtUnitPrice);
    
            // Label: Discount Rate
            lblDiscountRate = new Label();
            lblDiscountRate.AutoSize = true;
            lblDiscountRate.Location = new Point(200, 201);
            lblDiscountRate.Size = new System.Drawing.Size(78, 13);
            lblDiscountRate.TabIndex = 18;
            lblDiscountRate.Text = "Discount Rate:";
            Controls.Add(lblDiscountRate);
    
            // Text Box: Discount Rate
            txtDiscountRate = new TextBox(); 
            txtDiscountRate.Location = new Point(284, 198);
            txtDiscountRate.Size = new System.Drawing.Size(47, 20);
            txtDiscountRate.TabIndex = 19;
            txtDiscountRate.Text = "0.00";
            txtDiscountRate.TextAlign = HorizontalAlignment.Right;
            Controls.Add(txtDiscountRate);
    
            // Label: Discount Percent
            lblDiscountPercent = new Label();
            lblDiscountPercent.AutoSize = true;
            lblDiscountPercent.Location = new Point(334, 201);
            lblDiscountPercent.TabIndex = 20;
            lblDiscountPercent.Text = "%";
            Controls.Add(lblDiscountPercent);
    
            // Button: Submit
            btnSubmit = new Button();
            btnSubmit.Location = new Point(113, 236);
            btnSubmit.Size = new System.Drawing.Size(75, 23);
            btnSubmit.TabIndex = 25;
            btnSubmit.Text = "Submit";
            btnSubmit.Click += new System.EventHandler(btnSubmitClick);
            Controls.Add(btnSubmit);
    
            // Button: Reset
            btnReset = new Button();
            btnReset.Location = new Point(194, 236);
            btnReset.Size = new System.Drawing.Size(75, 23);
            btnReset.TabIndex = 26;
            btnReset.Text = "Reset";
            btnReset.Click += new System.EventHandler(btnResetClick);
            Controls.Add(btnReset);
    
            // Button: Close
            btnClose = new Button();
            btnClose.Location = new Point(275, 236);
            btnClose.Size = new System.Drawing.Size(75, 23);
            btnClose.TabIndex = 24;
            btnClose.Text = "Close";
            btnClose.Click += new System.EventHandler(btnCloseClick);
            Controls.Add(btnClose);
    
            // Dialog Box: New Store Item
            ClientSize = new System.Drawing.Size(372, 280);
            FormBorderStyle = FormBorderStyle.FixedDialog;
            MaximizeBox = false;
            MinimizeBox = false;
            ShowInTaskbar = false;
            StartPosition = FormStartPosition.CenterScreen;
            Text = "FunDS - New Store Item";
            Load += new System.EventHandler(StoreItemLoad);
        }
    
        private void PopulateStoreItem()
        {
            Random rndNumber = new Random();
            XmlDocument xdStoreItems = new XmlDocument();
            string strStoreItemsFile = @"C:\Fun Department Store\StoreItems.xml";
    
            // Set some default values on the New Store Item dialog box before the user opens it
            // Create a random item number. The user can change it if necessary
            txtItemNumber.Text = rndNumber.Next(100000, 999999).ToString();
    
            cbxManufacturers.Items.Clear();
            cbxCategories.Items.Clear();
            cbxSubCategories.Items.Clear();
    
            if (File.Exists(strStoreItemsFile))
            {
                using (FileStream fsStoreItems = new FileStream(strStoreItemsFile, FileMode.Open, FileAccess.Read))
                {
                    xdStoreItems.Load(fsStoreItems);
    
                    XmlNodeList xnlCategories = xdStoreItems.SelectNodes("/StoreItems/StoreItem/Category");
                    XmlNodeList xnlSubCategories = xdStoreItems.SelectNodes("/StoreItems/StoreItem/SubCategory");
                    XmlNodeList xnlManufacturers = xdStoreItems.SelectNodes("/StoreItems/StoreItem/Manufacturer");
    
                    foreach (XmlNode xnManufacturer in xnlManufacturers)
                    {
                        if (!cbxManufacturers.Items.Contains(xnManufacturer.InnerText))
                            cbxManufacturers.Items.Add(xnManufacturer.InnerText);
                    }
    
                    foreach (XmlNode xnCategory in xnlCategories)
                    {
                        if (!cbxCategories.Items.Contains(xnCategory.InnerText))
                            cbxCategories.Items.Add(xnCategory.InnerText);
                    }
    
                    foreach (XmlNode xnSubCategory in xnlSubCategories)
                    {
                        if (!cbxSubCategories.Items.Contains(xnSubCategory.InnerText))
                            cbxSubCategories.Items.Add(xnSubCategory.InnerText);
                    }
    
                    cbxManufacturers.Sorted = true;
                    cbxCategories.Sorted = true;
                    cbxSubCategories.Sorted = true;
    
                    cbxManufacturers.Text = "N/A";
                    cbxCategories.Text = "N/A";
                    cbxSubCategories.Text = "N/A";
                    txtItemName.Text = "";
                    txtItemSize.Text = "";
                    txtUnitPrice.Text = "0.00";
                    txtDiscountRate.Text = "";
                }
            }
        }
    
        private void StoreItemLoad(object sender, EventArgs e)
        {
            PopulateStoreItem();
        }
    
        private void btnNewManufacturerClick(object sender, EventArgs e)
        {
            // bool ManufacturerFound = false;
            Manufacturer man = new Manufacturer();
    
            // Show the New Manufacturer dialog box.
            // Find out if the user clicked OK after using the dialog box
            if (man.ShowDialog() == DialogResult.OK)
            {
                // If the user clicked OK...
                // ... if the Manufacturer text box was empty, do nothing
                if (string.IsNullOrEmpty(man.txtManufacturer.Text))
                {
                    MessageBox.Show("You must enter a manufacturer's name.",
                                    "FunDS - Fun Department Store",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    if (!cbxManufacturers.Items.Contains(man.txtManufacturer.Text))
                    {
                        cbxManufacturers.Items.Add(man.txtManufacturer.Text);
                        cbxManufacturers.Text = man.txtManufacturer.Text;
                    }
                }
            }
        }
    
        private void btnNewCategoryClick(object sender, EventArgs e)
        {
            Category cat = new Category();
    
            if (cat.ShowDialog() == DialogResult.OK)
            {
                if (string.IsNullOrEmpty(cat.txtCategory.Text))
                {
                    MessageBox.Show("You must enter a category's name.",
                                    "FunDS - Fun Department Store",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    if (!cbxCategories.Items.Contains(cat.txtCategory.Text))
                    {
                        cbxCategories.Items.Add(cat.txtCategory.Text);
                        cbxCategories.Text = cat.txtCategory.Text;
                    }
                }
            }
        }
    
        private void btnNewSubCategoryClick(object sender, EventArgs e)
        {
            SubCategory sc = new SubCategory();
    
            if (sc.ShowDialog() == DialogResult.OK)
            {
                if (string.IsNullOrEmpty(sc.txtSubCategory.Text))
                {
                    MessageBox.Show("You must enter a sub-category's name.",
                                    "FunDS - Fun Department Store",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    if (!cbxSubCategories.Items.Contains(sc.txtSubCategory.Text))
                    {
                        cbxSubCategories.Items.Add(sc.txtSubCategory.Text);
                        cbxSubCategories.Text = sc.txtSubCategory.Text;
                    }
                }
            }
        }
    
        private void btnResetClick(object sender, EventArgs e)
        {
            PopulateStoreItem();
        }
    
        private void btnSubmitClick(object sender, EventArgs e)
        {
            XmlDocument xdStoreItems = new XmlDocument();
            string strStoreItemsFile = @"C:\Fun Department Store\StoreItems.xml";
    
            if (string.IsNullOrEmpty(txtItemNumber.Text))
            {
                MessageBox.Show("You must provide at least an item number.",
                                "FunDS - Fun Department Store",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
                return;
            }
            else if (string.IsNullOrEmpty(txtItemName.Text))
            {
                MessageBox.Show("You must specify the name of the item or a (short) description.",
                                "FunDS - Fun Department Store",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
                return;
            }
    
            if (!File.Exists(strStoreItemsFile))
            {
                using (FileStream fsStoreItems = new FileStream(strStoreItemsFile, FileMode.Create, FileAccess.Write))
                {                
                xdStoreItems.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                                     "<StoreItems></StoreItems>");
                xdStoreItems.Save(fsStoreItems);
                }
            }
            using (FileStream fsStoreItems = new FileStream(strStoreItemsFile, FileMode.Open, FileAccess.Read))
            {
                xdStoreItems.Load(fsStoreItems);
            }
    
            using (FileStream fsStoreItems = new FileStream(strStoreItemsFile, FileMode.Create, FileAccess.Write))
            {
                XmlElement xeStoreItem = xdStoreItems.CreateElement("StoreItem");
                xeStoreItem.InnerXml = "<ItemNumber>" + txtItemNumber.Text + "</ItemNumber>" +
                                       "<Manufacturer>" + cbxManufacturers.Text + "</Manufacturer>" +
                                       "<Category>" + cbxCategories.Text + "</Category>" +
                                       "<SubCategory>" + cbxSubCategories.Text + "</SubCategory>" +
                                       "<ItemName>" + txtItemName.Text + "</ItemName>" +
                                       "<ItemSize>" + txtItemSize.Text + "</ItemSize>" +
                                       "<UnitPrice>" + txtUnitPrice.Text + "</UnitPrice>" +
                                       "<DiscountRate>" + txtDiscountRate.Text + "</DiscountRate>";
    
                xdStoreItems.DocumentElement.AppendChild(xeStoreItem);
    
                xdStoreItems.Save(fsStoreItems);
            }
    
            PopulateStoreItem();
        }
    
        private void btnCloseClick(object sender, EventArgs e)
        {
            Close();
        }
    }
  20. On the main menu, click File -> New
  21. When asked whether you want to save, click Save
  22. In the top combo box, make sure FunDS1 is selected Set the Save As Type combo box to All Files
  23. Set the File Name to StoreItemNew.cs
  24. Click Save

Editing a Store Item

Editing an item consists of changing any of its aspects. Since this is an XML-based application, you can simply open a file in a text editor and change anything. As for us, we will provide a form that can be used to enter an item number and click a button to find it. If the item is found, the employee can make any changes, and save it.

Practical LearningPractical Learning: Editing a Store Item

  1. In the empty document, type the following:
    using System;
    using System.IO;
    using System.Xml;
    using System.Drawing;
    using System.Windows.Forms;
    
    public class StoreItemEditor : Form
    {
        private Label lblItemNumber;
        public TextBox txtItemNumber;
        private Button btnFind;
        private Label lblManufacturer;
        public ComboBox cbxManufacturers;
        private Button btnNewManufacturer;
        private Label lblCategory;
        public ComboBox cbxCategories;
        private Button btnNewCategory;
        private Label lblSubCategory;
        public ComboBox cbxSubCategories;
        private Button btnNewSubCategory;
        private Label lblItemName;
        public TextBox txtItemName;
        private Label lblItemSize;
        public TextBox txtItemSize;
        private Label lblUnitPrice;
        public TextBox txtUnitPrice;
        private Label lblDiscountRate;
        public TextBox txtDiscountRate;
        private Label lblDiscountPercent;
        private Button btnUpdate;
        private Button btnClose;
    
        public StoreItemEditor()
        {
            InitializeComponent();
        }
    
        private void InitializeComponent()
        {
            // Label: Item Number
            lblItemNumber = new Label();
            lblItemNumber.AutoSize = true;
            lblItemNumber.Location = new Point(17, 24);
            lblItemNumber.TabIndex = 2;
            lblItemNumber.Text = "Item Number:";
            Controls.Add(lblItemNumber);
    
            txtItemNumber = new TextBox();
            txtItemNumber.Location = new Point(96, 21);
            txtItemNumber.Size = new System.Drawing.Size(75, 20);
            txtItemNumber.TabIndex = 3;
            Controls.Add(txtItemNumber);
    
            // Button: New Find
            btnFind = new Button();
            btnFind.Location = new Point(180, 20);
            btnFind.Size = new System.Drawing.Size(50, 23);
            btnFind.TabIndex = 21;
            btnFind.Text = "Find";
            btnFind.Click += new System.EventHandler(btnFindClick);
            Controls.Add(btnFind);
    
            // Label: Manufacturer
            lblManufacturer = new Label();
            lblManufacturer.AutoSize = true;
            lblManufacturer.Location = new Point(17, 54);
            lblManufacturer.TabIndex = 6;
            lblManufacturer.Text = "Manufacturer:";
            Controls.Add(lblManufacturer);
    
            // Combo Box: Manufacturers
            cbxManufacturers = new ComboBox();
            cbxManufacturers.Location = new Point(96, 50);
            cbxManufacturers.Size = new System.Drawing.Size(200, 21);
            cbxManufacturers.TabIndex = 7;
            Controls.Add(cbxManufacturers);
    
            // Button: New Manufacturer
            btnNewManufacturer = new Button();
            btnNewManufacturer.Location = new Point(302, 49);
            btnNewManufacturer.Size = new System.Drawing.Size(50, 23);
            btnNewManufacturer.TabIndex = 21;
            btnNewManufacturer.Text = "New...";
            btnNewManufacturer.Click += new System.EventHandler(btnNewManufacturerClick);
            Controls.Add(btnNewManufacturer);
    
            // Label: Category
            lblCategory = new Label();
            lblCategory.AutoSize = true;
            lblCategory.Location = new Point(17, 82);
            lblCategory.TabIndex = 8;
            lblCategory.Text = "Category:";
            Controls.Add(lblCategory);
    
            // Combo Box: Categories
            cbxCategories = new ComboBox();
            cbxCategories.Location = new Point(96, 79);
            cbxCategories.Size = new System.Drawing.Size(200, 21);
            cbxCategories.TabIndex = 9;
            Controls.Add(cbxCategories);
    
            // Button: New Category
            btnNewCategory = new Button();
            btnNewCategory.Location = new Point(302, 77);
            btnNewCategory.Size = new System.Drawing.Size(50, 23);
            btnNewCategory.TabIndex = 22;
            btnNewCategory.Text = "New...";
            btnNewCategory.Click += new System.EventHandler(btnNewCategoryClick);
            Controls.Add(btnNewCategory);
    
            // Label: Sub-Category
            lblSubCategory = new Label();
            lblSubCategory.AutoSize = true;
            lblSubCategory.Location = new Point(17, 113);
            lblSubCategory.TabIndex = 10;
            lblSubCategory.Text = "Sub-Category:";
            Controls.Add(lblSubCategory);
    
            // Combo Box: Sub-Categories
            cbxSubCategories = new ComboBox();
            cbxSubCategories.Location = new Point(96, 109);
            cbxSubCategories.Size = new System.Drawing.Size(200, 21);
            cbxSubCategories.TabIndex = 11;
            Controls.Add(cbxSubCategories);
    
            // Button: New Sub-Category
            btnNewSubCategory = new Button();
            btnNewSubCategory.Location = new Point(302, 107);
            btnNewSubCategory.Size = new System.Drawing.Size(50, 23);
            btnNewSubCategory.TabIndex = 23;
            btnNewSubCategory.Text = "New...";
            btnNewSubCategory.Click += new System.EventHandler(btnNewSubCategoryClick);
            Controls.Add(btnNewSubCategory);
    
            // Label: Item Name
            lblItemName = new Label();
            lblItemName.AutoSize = true;
            lblItemName.Location = new Point(17, 142);
            lblItemName.TabIndex = 12;
            lblItemName.Text = "Item Name:";
            Controls.Add(lblItemName);
    
            // Text Box: Item Name
            txtItemName = new TextBox();
            txtItemName.Location = new Point(96, 139);
            txtItemName.Size = new System.Drawing.Size(256, 20);
            txtItemName.TabIndex = 13;
            Controls.Add(txtItemName);
    
            // Label: Item Size
            lblItemSize = new Label();
            lblItemSize.AutoSize = true;
            lblItemSize.Location = new Point(17, 172);
            lblItemSize.TabIndex = 14;
            lblItemSize.Text = "Item Size:";
            Controls.Add(lblItemSize);
    
            // Text Box: Item Size
            txtItemSize = new TextBox();
            txtItemSize.Location = new Point(96, 169);
            txtItemSize.Size = new System.Drawing.Size(256, 20);
            txtItemSize.TabIndex = 15;
            Controls.Add(txtItemSize);
    
            // Label: Unit Price
            lblUnitPrice = new Label();
            lblUnitPrice.AutoSize = true;
            lblUnitPrice.Location = new Point(17, 201);
            lblUnitPrice.TabIndex = 16;
            lblUnitPrice.Text = "Unit Price:";
            Controls.Add(lblUnitPrice);
    
            // Text Box: Unit Price
            txtUnitPrice = new TextBox();
            txtUnitPrice.Location = new Point(96, 198);
            txtUnitPrice.Size = new System.Drawing.Size(94, 20);
            txtUnitPrice.TabIndex = 17;
            txtUnitPrice.Text = "0.00";
            txtUnitPrice.TextAlign = HorizontalAlignment.Right;
            Controls.Add(txtUnitPrice);
    
            // Label: Discount Rate
            lblDiscountRate = new Label(); 
            lblDiscountRate.AutoSize = true;
            lblDiscountRate.Location = new Point(200, 201);
            lblDiscountRate.Size = new System.Drawing.Size(78, 13);
            lblDiscountRate.TabIndex = 18;
            lblDiscountRate.Text = "Discount Rate:";
            Controls.Add(lblDiscountRate);
    
            // Text Box: Discount Rate
            txtDiscountRate = new TextBox();
            txtDiscountRate.Location = new Point(284, 198);
            txtDiscountRate.Size = new System.Drawing.Size(47, 20);
            txtDiscountRate.TabIndex = 19;
            txtDiscountRate.Text = "0.00";
            txtDiscountRate.TextAlign = HorizontalAlignment.Right;
            Controls.Add(txtDiscountRate);
    
            // Label: Discount Percent
            lblDiscountPercent = new Label();
            lblDiscountPercent.AutoSize = true;
            lblDiscountPercent.Location = new Point(334, 201);
            lblDiscountPercent.TabIndex = 20;
            lblDiscountPercent.Text = "%";
            Controls.Add(lblDiscountPercent);
    
            // Button: Update
            btnUpdate = new Button();
            btnUpdate.Location = new Point(196, 231);
            btnUpdate.Size = new System.Drawing.Size(75, 23);
            btnUpdate.TabIndex = 25;
            btnUpdate.Text = "Update";
            btnUpdate.Click += new System.EventHandler(btnUpdateClick);
            Controls.Add(btnUpdate);
    
            // Button: Close
            btnClose = new Button();
            btnClose.Location = new Point(275, 231);
            btnClose.Size = new System.Drawing.Size(75, 23);
            btnClose.TabIndex = 24;
            btnClose.Text = "Close";
            btnClose.Click += new System.EventHandler(btnCloseClick);
            Controls.Add(btnClose);
    
            // Dialog Box: New Store Item
            ClientSize = new System.Drawing.Size(372, 276);
            FormBorderStyle = FormBorderStyle.FixedDialog;
            MaximizeBox = false;
            MinimizeBox = false;
            ShowInTaskbar = false;
            StartPosition = FormStartPosition.CenterScreen;
            Text = "FunDS - Edit Store Item";
            Load += new System.EventHandler(StoreItemLoad);
        }
    
        private void PopulateStoreItem()
        {
            XmlDocument xdStoreItems = new XmlDocument();
            string strStoreItemsFile = @"C:\Fun Department Store\StoreItems.xml";
    
            cbxManufacturers.Items.Clear();
            cbxCategories.Items.Clear();
            cbxSubCategories.Items.Clear();
    
            if (File.Exists(strStoreItemsFile))
            {
                using (FileStream fsStoreItems = new FileStream(strStoreItemsFile, FileMode.Open, FileAccess.Read))
                {
                    xdStoreItems.Load(fsStoreItems);
    
                    XmlNodeList xnlCategories = xdStoreItems.SelectNodes("/StoreItems/StoreItem/Category");
                    XmlNodeList xnlSubCategories = xdStoreItems.SelectNodes("/StoreItems/StoreItem/SubCategory");
                    XmlNodeList xnlManufacturers = xdStoreItems.SelectNodes("/StoreItems/StoreItem/Manufacturer");
    
                    foreach (XmlNode xnManufacturer in xnlManufacturers)
                    {
                        if (!cbxManufacturers.Items.Contains(xnManufacturer.InnerText))
                            cbxManufacturers.Items.Add(xnManufacturer.InnerText);
                    }
    
                    foreach (XmlNode xnCategory in xnlCategories)
                    {
                        if (!cbxCategories.Items.Contains(xnCategory.InnerText))
                            cbxCategories.Items.Add(xnCategory.InnerText);
                    }
    
                    foreach (XmlNode xnSubCategory in xnlSubCategories)
                    {
                        if (!cbxSubCategories.Items.Contains(xnSubCategory.InnerText))
                            cbxSubCategories.Items.Add(xnSubCategory.InnerText);
                    }
    
                    cbxManufacturers.Sorted = true;
                    cbxCategories.Sorted = true;
                    cbxSubCategories.Sorted = true;/**/
    
                    cbxManufacturers.Text = "N/A";
                    cbxCategories.Text = "N/A";
                    cbxSubCategories.Text = "N/A";
                    txtItemName.Text = "";
                    txtItemSize.Text = "";
                    txtUnitPrice.Text = "0.00";
                    txtDiscountRate.Text = "0.00";
                }
            }
        }
    
        private void StoreItemLoad(object sender, EventArgs e)
        {
            PopulateStoreItem();
        }
    
        private void btnNewManufacturerClick(object sender, EventArgs e)
        {
            // bool ManufacturerFound = false;
            Manufacturer man = new Manufacturer();
    
            // Show the New Manufacturer dialog box.
            // Find out if the user clicked OK after using the dialog box
            if (man.ShowDialog() == DialogResult.OK)
            {
                // If the user clicked OK...
                // ... if the Manufacturer text box was empty, do nothing
                if (string.IsNullOrEmpty(man.txtManufacturer.Text))
                {
                    MessageBox.Show("You must enter a manufacturer's name.",
                                    "FunDS - Fun Department Store",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    if (!cbxManufacturers.Items.Contains(man.txtManufacturer.Text))
                    {
                        cbxManufacturers.Items.Add(man.txtManufacturer.Text);
                        cbxManufacturers.Text = man.txtManufacturer.Text;
                    }
                }
            }
        }
    
        private void btnNewCategoryClick(object sender, EventArgs e)
        {
            Category cat = new Category();
    
            if (cat.ShowDialog() == DialogResult.OK)
            {
                if (string.IsNullOrEmpty(cat.txtCategory.Text))
                {
                    MessageBox.Show("You must enter a category's name.",
                                    "FunDS - Fun Department Store",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    if (!cbxCategories.Items.Contains(cat.txtCategory.Text))
                    {
                        cbxCategories.Items.Add(cat.txtCategory.Text);
                        cbxCategories.Text = cat.txtCategory.Text;
                    }
                }
            }
        }
    
        private void btnNewSubCategoryClick(object sender, EventArgs e)
        {
            SubCategory sc = new SubCategory();
    
            if (sc.ShowDialog() == DialogResult.OK)
            {
                if (string.IsNullOrEmpty(sc.txtSubCategory.Text))
                {
                    MessageBox.Show("You must enter a sub-category's name.",
                                    "FunDS - Fun Department Store",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    if (!cbxSubCategories.Items.Contains(sc.txtSubCategory.Text))
                    {
                        cbxSubCategories.Items.Add(sc.txtSubCategory.Text);
                        cbxSubCategories.Text = sc.txtSubCategory.Text;
                    }
                }
            }
        }
    
        private void btnFindClick(object sender, EventArgs e)
        {
            bool itemFound = false;
            XmlDocument xdStoreItems = new XmlDocument();
            string strStoreItemsFile = @"C:\Fun Department Store\StoreItems1.xml";
    
            if (string.IsNullOrEmpty(txtItemNumber.Text))
            {
                MessageBox.Show("You must provide an item number.",
                                "FunDS - Fun Department Store",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
                return;
            }
    
            if (File.Exists(strStoreItemsFile))
            {
                using (FileStream fsStoreItems = new FileStream(strStoreItemsFile, FileMode.Open, FileAccess.Read))
                {
                    xdStoreItems.Load(fsStoreItems);
    
                    XmlNodeList xnlStoreItems = xdStoreItems.GetElementsByTagName("ItemNumber");
    
                    foreach (XmlNode xnStoreItem in xnlStoreItems)
                    {
                        if (xnStoreItem.InnerText == txtItemNumber.Text)
                        {
                            itemFound = true;
                            cbxManufacturers.Text = xnStoreItem.NextSibling.InnerText;
                            cbxCategories.Text    = xnStoreItem.NextSibling.NextSibling.InnerText;
                            cbxSubCategories.Text = xnStoreItem.NextSibling.NextSibling.NextSibling.InnerText;
                            txtItemName.Text      = xnStoreItem.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                            txtItemSize.Text      = xnStoreItem.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                            txtUnitPrice.Text     = xnStoreItem.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                            txtDiscountRate.Text  = xnStoreItem.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
    
                            return;
                        }
                    }
    
                    if (itemFound == false)
                        MessageBox.Show("There is no item with that number.",
                                        "FunDS - Fun Department Store",
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Information);
                }
            }
        }
    
        private void btnUpdateClick(object sender, EventArgs e)
        {
            bool itemFound = false;
            XmlDocument xdStoreItems = new XmlDocument();
            string strStoreItemsFile = @"C:\Fun Department Store\StoreItems.xml";
    
            if (string.IsNullOrEmpty(txtItemNumber.Text))
            {
                MessageBox.Show("You must provide an item number.",
                                "FunDS - Fun Department Store",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
                return;
            }
    
            if (File.Exists(strStoreItemsFile))
            {
                using (FileStream fsStoreItems = new FileStream(strStoreItemsFile, FileMode.Open, FileAccess.Read))
                {
                    xdStoreItems.Load(fsStoreItems);
    
                    XmlNodeList xnlStoreItems = xdStoreItems.GetElementsByTagName("ItemNumber");
    
                    foreach (XmlNode xnStoreItem in xnlStoreItems)
                    {
                        if (xnStoreItem.InnerText == txtItemNumber.Text)
                        {
                            itemFound = true;
                            xnStoreItem.ParentNode.InnerXml = "<ItemNumber>" + txtItemNumber.Text + "</ItemNumber>" +
                                                              "<Manufacturer>" + cbxManufacturers.Text + "</Manufacturer>" +
                                                              "<Category>" + cbxCategories.Text + "</Category>" +
                                                              "<SubCategory>" + cbxSubCategories.Text + "</SubCategory>" +
                                                              "<ItemName>" + txtItemName.Text + "</ItemName>" +
                                                              "<ItemSize>" + txtItemSize.Text + "</ItemSize>" +
                                                              "<UnitPrice>" + txtUnitPrice.Text + "</UnitPrice>" +
                                                              "<DiscountRate>" + txtDiscountRate.Text + "</DiscountRate>"; ;
                            break;
                        }
                    }
                }
    
                if (itemFound == true)
                {
                    using (FileStream fsStoreItems = new FileStream(strStoreItemsFile, FileMode.Create, FileAccess.Write))
                    {
                        xdStoreItems.Save(fsStoreItems);
                        MessageBox.Show("The store item has been updated.",
                                        "FunDS - Fun Department Store",
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Information);
                    }
                }
            }
    
            PopulateStoreItem();
        }
    
        private void btnCloseClick(object sender, EventArgs e)
        {
            Close();
        }
    }
  2. On the main menu, click File -> New
  3. When asked whether you want to save, click Save
  4. In the top combo box, make sure the FunDS1 folder is displaying, Set the Save As Type to All Files
  5. Set the name to StoreItemEditor.cs
  6. Click Save

Deleting a Store Item

To make sure we cover most aspects of data maintenance are covered, we will give the company the ability to delete an item from the inventory.

Practical LearningPractical Learning: Deleting an Item

  1. In the empty document, type the following:
    using System;
    using System.IO;
    using System.Xml;
    using System.Drawing;
    using System.Windows.Forms;
    
    public class StoreItemDelete : Form
    {
        private Label lblItemNumber;
        public TextBox txtItemNumber;
        private Button btnFind;
        private Label lblManufacturer;
        public ComboBox cbxManufacturers;
        private Button btnNewManufacturer;
        private Label lblCategory;
        public ComboBox cbxCategories;
        private Button btnNewCategory;
        private Label lblSubCategory;
        public ComboBox cbxSubCategories;
        private Button btnNewSubCategory;
        private Label lblItemName;
        public TextBox txtItemName;
        private Label lblItemSize;
        public TextBox txtItemSize;
        private Label lblUnitPrice;
        public TextBox txtUnitPrice;
        private Label lblDiscountRate;
        public TextBox txtDiscountRate;
        private Label lblDiscountPercent;
        private Button btnDelete;
        private Button btnClose;
    
        public StoreItemDelete()
        {
            InitializeComponent();
        }
    
        private void InitializeComponent()
        {
            // Label: Item Number
            lblItemNumber = new Label();
            lblItemNumber.AutoSize = true;
            lblItemNumber.Location = new Point(17, 24);
            lblItemNumber.TabIndex = 2;
            lblItemNumber.Text = "Item Number:";
            Controls.Add(lblItemNumber);
    
            txtItemNumber = new TextBox();
            txtItemNumber.Location = new Point(96, 21);
            txtItemNumber.Size = new System.Drawing.Size(75, 20);
            txtItemNumber.TabIndex = 3;
            Controls.Add(txtItemNumber);
    
            // Button: New Find
            btnFind = new Button();
            btnFind.Location = new Point(180, 20);
            btnFind.Size = new System.Drawing.Size(50, 23);
            btnFind.TabIndex = 21;
            btnFind.Text = "Find";
            btnFind.Click += new System.EventHandler(btnFindClick);
            Controls.Add(btnFind);
    
            // Label: Manufacturer
            lblManufacturer = new Label();
            lblManufacturer.AutoSize = true;
            lblManufacturer.Location = new Point(17, 54);
            lblManufacturer.TabIndex = 6;
            lblManufacturer.Text = "Manufacturer:";
            Controls.Add(lblManufacturer);
    
            // Combo Box: Manufacturers
            cbxManufacturers = new ComboBox();
            cbxManufacturers.Location = new Point(96, 50);
            cbxManufacturers.Size = new System.Drawing.Size(200, 21);
            cbxManufacturers.TabIndex = 7;
            Controls.Add(cbxManufacturers);
    
            // Button: New Manufacturer
            btnNewManufacturer = new Button();
            btnNewManufacturer.Location = new Point(302, 49);
            btnNewManufacturer.Size = new System.Drawing.Size(50, 23);
            btnNewManufacturer.TabIndex = 21;
            btnNewManufacturer.Text = "New...";
            btnNewManufacturer.Click += new System.EventHandler(btnNewManufacturerClick);
            Controls.Add(btnNewManufacturer);
    
            // Label: Category
            lblCategory = new Label();
            lblCategory.AutoSize = true;
            lblCategory.Location = new Point(17, 82);
            lblCategory.TabIndex = 8;
            lblCategory.Text = "Category:";
            Controls.Add(lblCategory);
    
            // Combo Box: Categories
            cbxCategories = new ComboBox();
            cbxCategories.Location = new Point(96, 79);
            cbxCategories.Size = new System.Drawing.Size(200, 21);
            cbxCategories.TabIndex = 9;
            Controls.Add(cbxCategories);
    
            // Button: New Category
            btnNewCategory = new Button();
            btnNewCategory.Location = new Point(302, 77);
            btnNewCategory.Size = new System.Drawing.Size(50, 23);
            btnNewCategory.TabIndex = 22;
            btnNewCategory.Text = "New...";
            btnNewCategory.Click += new System.EventHandler(btnNewCategoryClick);
            Controls.Add(btnNewCategory);
    
            // Label: Sub-Category
            lblSubCategory = new Label();
            lblSubCategory.AutoSize = true;
            lblSubCategory.Location = new Point(17, 113);
            lblSubCategory.TabIndex = 10;
            lblSubCategory.Text = "Sub-Category:";
            Controls.Add(lblSubCategory);
    
            // Combo Box: Sub-Categories
            cbxSubCategories = new ComboBox();
            cbxSubCategories.Location = new Point(96, 109);
            cbxSubCategories.Size = new System.Drawing.Size(200, 21);
            cbxSubCategories.TabIndex = 11;
            Controls.Add(cbxSubCategories);
    
            // Button: New Sub-Category
            btnNewSubCategory = new Button();
            btnNewSubCategory.Location = new Point(302, 107);
            btnNewSubCategory.Size = new System.Drawing.Size(50, 23);
            btnNewSubCategory.TabIndex = 23;
            btnNewSubCategory.Text = "New...";
            btnNewSubCategory.Click += new System.EventHandler(btnNewSubCategoryClick);
            Controls.Add(btnNewSubCategory);
    
            // Label: Item Name
            lblItemName = new Label();
            lblItemName.AutoSize = true;
            lblItemName.Location = new Point(17, 142);
            lblItemName.TabIndex = 12;
            lblItemName.Text = "Item Name:";
            Controls.Add(lblItemName);
    
            // Text Box: Item Name
            txtItemName = new TextBox();
            txtItemName.Location = new Point(96, 139);
            txtItemName.Size = new System.Drawing.Size(256, 20);
            txtItemName.TabIndex = 13;
            Controls.Add(txtItemName);
    
            // Label: Item Size
            lblItemSize = new Label();
            lblItemSize.AutoSize = true;
            lblItemSize.Location = new Point(17, 172);
            lblItemSize.TabIndex = 14;
            lblItemSize.Text = "Item Size:";
            Controls.Add(lblItemSize);
    
            // Text Box: Item Size
            txtItemSize = new TextBox();
            txtItemSize.Location = new Point(96, 169);
            txtItemSize.Size = new System.Drawing.Size(256, 20);
            txtItemSize.TabIndex = 15;
            Controls.Add(txtItemSize);
    
            // Label: Unit Price
            lblUnitPrice = new Label();
            lblUnitPrice.AutoSize = true;
            lblUnitPrice.Location = new Point(17, 201);
            lblUnitPrice.TabIndex = 16;
            lblUnitPrice.Text = "Unit Price:";
            Controls.Add(lblUnitPrice);
    
            // Text Box: Unit Price
            txtUnitPrice = new TextBox();
            txtUnitPrice.Location = new Point(96, 198);
            txtUnitPrice.Size = new System.Drawing.Size(94, 20);
            txtUnitPrice.TabIndex = 17;
            txtUnitPrice.Text = "0.00";
            txtUnitPrice.TextAlign = HorizontalAlignment.Right;
            Controls.Add(txtUnitPrice);
    
            // Label: Discount Rate
            lblDiscountRate = new Label();
            lblDiscountRate.AutoSize = true;
            lblDiscountRate.Location = new Point(200, 201);
            lblDiscountRate.Size = new System.Drawing.Size(78, 13);
            lblDiscountRate.TabIndex = 18;
            lblDiscountRate.Text = "Discount Rate:";
            Controls.Add(lblDiscountRate);
    
            // Text Box: Discount Rate
            txtDiscountRate = new TextBox();
            txtDiscountRate.Location = new Point(284, 198);
            txtDiscountRate.Size = new System.Drawing.Size(47, 20);
            txtDiscountRate.TabIndex = 19;
            txtDiscountRate.Text = "0.00";
            txtDiscountRate.TextAlign = HorizontalAlignment.Right;
            Controls.Add(txtDiscountRate);
    
            // Label: Discount Percent
            lblDiscountPercent = new Label();
            lblDiscountPercent.AutoSize = true;
            lblDiscountPercent.Location = new Point(334, 201);
            lblDiscountPercent.TabIndex = 20;
            lblDiscountPercent.Text = "%";
            Controls.Add(lblDiscountPercent);
    
            // Button: Delete
            btnDelete = new Button();
            btnDelete.Location = new Point(196, 231);
            btnDelete.Size = new System.Drawing.Size(75, 23);
            btnDelete.TabIndex = 25;
            btnDelete.Text = "Delete";
            btnDelete.Click += new System.EventHandler(btnDeleteClick);
            Controls.Add(btnDelete);
    
            // Button: Close
            btnClose = new Button();
            btnClose.Location = new Point(275, 231);
            btnClose.Size = new System.Drawing.Size(75, 23);
            btnClose.TabIndex = 24;
            btnClose.Text = "Close";
            btnClose.Click += new System.EventHandler(btnCloseClick);
            Controls.Add(btnClose);
    
            // Dialog Box: New Store Item
            ClientSize = new System.Drawing.Size(372, 276);
            FormBorderStyle = FormBorderStyle.FixedDialog;
            MaximizeBox = false;
            MinimizeBox = false;
            ShowInTaskbar = false;
            StartPosition = FormStartPosition.CenterScreen;
            Text = "FunDS - Edit Store Item";
            Load += new System.EventHandler(StoreItemLoad);
        }
    
        private void PopulateStoreItem()
        {
            XmlDocument xdStoreItems = new XmlDocument();
            string strStoreItemsFile = @"C:\Fun Department Store\StoreItems.xml";
    
            cbxManufacturers.Items.Clear();
            cbxCategories.Items.Clear();
            cbxSubCategories.Items.Clear();
    
            if (File.Exists(strStoreItemsFile))
            {
                using (FileStream fsStoreItems = new FileStream(strStoreItemsFile, FileMode.Open, FileAccess.Read))
                {
                    xdStoreItems.Load(fsStoreItems);
    
                    XmlNodeList xnlCategories = xdStoreItems.SelectNodes("/StoreItems/StoreItem/Category");
                    XmlNodeList xnlSubCategories = xdStoreItems.SelectNodes("/StoreItems/StoreItem/SubCategory");
                    XmlNodeList xnlManufacturers = xdStoreItems.SelectNodes("/StoreItems/StoreItem/Manufacturer");
    
                    foreach (XmlNode xnManufacturer in xnlManufacturers)
                    {
                        if (!cbxManufacturers.Items.Contains(xnManufacturer.InnerText))
                            cbxManufacturers.Items.Add(xnManufacturer.InnerText);
                    }
    
                    foreach (XmlNode xnCategory in xnlCategories)
                    {
                        if (!cbxCategories.Items.Contains(xnCategory.InnerText))
                            cbxCategories.Items.Add(xnCategory.InnerText);
                    }
    
                    foreach (XmlNode xnSubCategory in xnlSubCategories)
                    {
                        if (!cbxSubCategories.Items.Contains(xnSubCategory.InnerText))
                            cbxSubCategories.Items.Add(xnSubCategory.InnerText);
                    }
    
                    cbxManufacturers.Sorted = true;
                    cbxCategories.Sorted = true;
                    cbxSubCategories.Sorted = true;/**/
    
                    cbxManufacturers.Text = "N/A";
                    cbxCategories.Text = "N/A";
                    cbxSubCategories.Text = "N/A";
                    txtItemName.Text = "";
                    txtItemSize.Text = "";
                    txtUnitPrice.Text = "0.00";
                    txtDiscountRate.Text = "0.00";
                }
            }
        }
    
        private void StoreItemLoad(object sender, EventArgs e)
        {
            PopulateStoreItem();
        }
    
        private void btnNewManufacturerClick(object sender, EventArgs e)
        {
            // bool ManufacturerFound = false;
            Manufacturer man = new Manufacturer();
    
            // Show the New Manufacturer dialog box.
            // Find out if the user clicked OK after using the dialog box
            if (man.ShowDialog() == DialogResult.OK)
            {
                // If the user clicked OK...
                // ... if the Manufacturer text box was empty, do nothing
                if (string.IsNullOrEmpty(man.txtManufacturer.Text))
                {
                    MessageBox.Show("You must enter a manufacturer's name.",
                                    "FunDS - Fun Department Store",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    if (!cbxManufacturers.Items.Contains(man.txtManufacturer.Text))
                    {
                        cbxManufacturers.Items.Add(man.txtManufacturer.Text);
                        cbxManufacturers.Text = man.txtManufacturer.Text;
                    }
                }
            }
        }
    
        private void btnNewCategoryClick(object sender, EventArgs e)
        {
            Category cat = new Category();
    
            if (cat.ShowDialog() == DialogResult.OK)
            {
                if (string.IsNullOrEmpty(cat.txtCategory.Text))
                {
                    MessageBox.Show("You must enter a category's name.",
                                    "FunDS - Fun Department Store",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    if (!cbxCategories.Items.Contains(cat.txtCategory.Text))
                    {
                        cbxCategories.Items.Add(cat.txtCategory.Text);
                        cbxCategories.Text = cat.txtCategory.Text;
                    }
                }
            }
        }
    
        private void btnNewSubCategoryClick(object sender, EventArgs e)
        {
            SubCategory sc = new SubCategory();
    
            if (sc.ShowDialog() == DialogResult.OK)
            {
                if (string.IsNullOrEmpty(sc.txtSubCategory.Text))
                {
                    MessageBox.Show("You must enter a sub-category's name.",
                                    "FunDS - Fun Department Store",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    if (!cbxSubCategories.Items.Contains(sc.txtSubCategory.Text))
                    {
                        cbxSubCategories.Items.Add(sc.txtSubCategory.Text);
                        cbxSubCategories.Text = sc.txtSubCategory.Text;
                    }
                }
            }
        }
    
        private void btnFindClick(object sender, EventArgs e)
        {
            bool itemFound = false;
            XmlDocument xdStoreItems = new XmlDocument();
            string strStoreItemsFile = @"C:\Fun Department Store\StoreItems1.xml";
    
            if (string.IsNullOrEmpty(txtItemNumber.Text))
            {
                MessageBox.Show("You must provide an item number.",
                                "FunDS - Fun Department Store",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
                return;
            }
    
            if (File.Exists(strStoreItemsFile))
            {
                using (FileStream fsStoreItems = new FileStream(strStoreItemsFile, FileMode.Open, FileAccess.Read))
                {
                    xdStoreItems.Load(fsStoreItems);
    
                    XmlNodeList xnlStoreItems = xdStoreItems.GetElementsByTagName("ItemNumber");
    
                    foreach (XmlNode xnStoreItem in xnlStoreItems)
                    {
                        if (xnStoreItem.InnerText == txtItemNumber.Text)
                        {
                            itemFound = true;
                            cbxManufacturers.Text = xnStoreItem.NextSibling.InnerText;
                            cbxCategories.Text = xnStoreItem.NextSibling.NextSibling.InnerText;
                            cbxSubCategories.Text = xnStoreItem.NextSibling.NextSibling.NextSibling.InnerText;
                            txtItemName.Text = xnStoreItem.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                            txtItemSize.Text = xnStoreItem.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                            txtUnitPrice.Text = xnStoreItem.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                            txtDiscountRate.Text = xnStoreItem.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
    
                            return;
                        }
                    }
    
                    if (itemFound == false)
                        MessageBox.Show("There is no item with that number.",
                                        "FunDS - Fun Department Store",
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Information);
                }
            }
        }
    
        private void btnDeleteClick(object sender, EventArgs e)
        {
            bool itemFound = false;
            XmlDocument xdStoreItems = new XmlDocument();
            string strStoreItemsFile = @"C:\Fun Department Store\StoreItems1.xml";
    
            if (string.IsNullOrEmpty(txtItemNumber.Text))
            {
                MessageBox.Show("You must provide an item number.",
                                "FunDS - Fun Department Store",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
                return;
            }
    
            if (File.Exists(strStoreItemsFile))
            {
                using (FileStream fsStoreItems = new FileStream(strStoreItemsFile, FileMode.Open, FileAccess.Read))
                {
                    xdStoreItems.Load(fsStoreItems);
    
                    XmlNodeList xnlStoreItems = xdStoreItems.GetElementsByTagName("ItemNumber");
    
                    foreach (XmlNode xnStoreItem in xnlStoreItems)
                    {
                        if (xnStoreItem.InnerText == txtItemNumber.Text)
                        {
                            itemFound = true;
                            xdStoreItems.DocumentElement.RemoveChild(xnStoreItem.ParentNode);
                            break;
                        }
                    }
                }
    
                if (itemFound == true)
                {
                    using (FileStream fsStoreItems = new FileStream(strStoreItemsFile, FileMode.Create, FileAccess.Write))
                    {
                        xdStoreItems.Save(fsStoreItems);
                        MessageBox.Show("The store item has been deleted.",
                                        "FunDS - Fun Department Store",
                                        MessageBoxButtons.OK,
                                        MessageBoxIcon.Information);
                    }
                }
            }
    
            PopulateStoreItem();
        }
    
        private void btnCloseClick(object sender, EventArgs e)
        {
            Close();
        }
    }
  2. On the main menu, click File -> New
  3. When asked whether you want to save, click Save
  4. In the top combo box, make sure the FunDS1 folder is displaying, Set the Save As Type to All Files
  5. Set the File Name to StoreItemDelete.cs
  6. Click Save
  7. In the empty document, type the following:
    using System;
    using System.IO;
    using System.Xml;
    using System.Drawing;
    using System.Windows.Forms;
    
    public class StoreItems : Form
    {
        private ColumnHeader colStoreItemID;
        private ColumnHeader colItemNumber;
        private ColumnHeader colManufacturer;
        private ColumnHeader colCategory;
        private ColumnHeader colSubCategory;
        private ColumnHeader colItemName;
        private ColumnHeader colItemSize;
        private ColumnHeader colUnitPrice;
        private ColumnHeader colDiscountRate;
        private ColumnHeader colDiscountAmount;
        private ColumnHeader colMarkedPrice;
        private ListView lvwStoreItems;
        private Label lblNumberOfItems;
        private TextBox txtNumberOfItems;
        private Button btnNewStoreItem;
        private Button btnEditStoreItem;
        private Button btnDeleteStoreItem;
        private Button btnClose;
    
        public StoreItems()
        {
            InitializeComponent();
        }
    
        private void InitializeComponent()
        {
            // Column: umn: Store Item ID
            colStoreItemID = new ColumnHeader();
            colStoreItemID.Text = "Item ID";
            colStoreItemID.Width = 50;
    
            // Column: umn: Item Number
            colItemNumber = new ColumnHeader();
            colItemNumber.Text = "Item #";
            colItemNumber.TextAlign = HorizontalAlignment.Center;
    
            // Column: umn: Manufacturer
            colManufacturer = new ColumnHeader();
            colManufacturer.Text = "Manufacturer";
            colManufacturer.Width = 130;
    
            // Column: umn: Category
            colCategory = new ColumnHeader();
            colCategory.Text = "Category";
            colCategory.Width = 70;
    
            // Column: umn: Sub-Category
            colSubCategory = new ColumnHeader();
            colSubCategory.Text = "Sub-Category";
            colSubCategory.Width = 80;
    
            // Column: umn: Item Name
            colItemName = new ColumnHeader();
            colItemName.Text = "Item Name";
            colItemName.Width = 300;
    
            // Column: umn: Item Size
            colItemSize = new ColumnHeader();
            colItemSize.Text = "Size";
            colItemSize.Width = 80;
    
            // Column: umn: Unit Price
            colUnitPrice = new ColumnHeader();
            colUnitPrice.Text = "Unit Price";
            colUnitPrice.TextAlign = HorizontalAlignment.Right;
    
            // Column: umn: Discount Rate
            colDiscountRate = new ColumnHeader();
            colDiscountRate.Text = "Disc Rate";
            colDiscountRate.TextAlign = HorizontalAlignment.Right;
            colDiscountRate.Width = 60;
    
            // Column: umn: Discount Amount
            colDiscountAmount = new ColumnHeader();
            colDiscountAmount.Text = "Disc Amt";
            colDiscountAmount.TextAlign = HorizontalAlignment.Right;
    
            // Column: umn: Marked Price
            colMarkedPrice = new ColumnHeader();
            colMarkedPrice.Text = "Marked Price";
            colMarkedPrice.TextAlign = HorizontalAlignment.Right;
            colMarkedPrice.Width = 75;
    
            lvwStoreItems = new ListView();
            lvwStoreItems.Anchor = AnchorStyles.Top | AnchorStyles.Bottom
                             | AnchorStyles.Left | AnchorStyles.Right;
            lvwStoreItems.Columns.AddRange(new ColumnHeader[]
            {
                colStoreItemID,	colItemNumber, colManufacturer,
                colCategory, colSubCategory, colItemName, colItemSize,
                colUnitPrice, colDiscountRate, colDiscountAmount, colMarkedPrice
            });
            lvwStoreItems.FullRowSelect = true;
            lvwStoreItems.GridLines = true;
            lvwStoreItems.Location = new Point(15, 15);
            lvwStoreItems.Size = new System.Drawing.Size(1050, 340);
            lvwStoreItems.TabIndex = 3;
            lvwStoreItems.View = View.Details;
    
            lblNumberOfItems = new Label();
            lblNumberOfItems.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
            lblNumberOfItems.AutoSize = true;
            lblNumberOfItems.Location = new Point(15, 370);
            lblNumberOfItems.TabIndex = 6;
            lblNumberOfItems.Text = "Number of Items:";
    
            // Text Box: Number of Items
            txtNumberOfItems = new TextBox();
            txtNumberOfItems.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
            txtNumberOfItems.Location = new Point(111, 368);
            txtNumberOfItems.Size = new System.Drawing.Size(60, 20);
            txtNumberOfItems.TabIndex = 7;
            txtNumberOfItems.TextAlign = HorizontalAlignment.Right;
    
            btnNewStoreItem = new Button();
            btnNewStoreItem.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
            btnNewStoreItem.Location = new Point(530, 366);
            btnNewStoreItem.Size = new System.Drawing.Size(125, 32);
            btnNewStoreItem.TabIndex = 5;
            btnNewStoreItem.Text = "New Store Item ...";
            btnNewStoreItem.Click += new System.EventHandler(btnNewStoreItemClick);
    
            // Button: Edit Item
            btnEditStoreItem = new Button();
            btnEditStoreItem.Location = new Point(659, 366);
            btnEditStoreItem.Size = new System.Drawing.Size(125, 32);
            btnEditStoreItem.Text = "Edit Store Item...";
            btnEditStoreItem.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
            btnEditStoreItem.Click += new System.EventHandler(btnEditStoreItemClick);
    
            // Button: Remove Item
            btnDeleteStoreItem = new Button();
            btnDeleteStoreItem.Location = new Point(792, 366);
            btnDeleteStoreItem.Size = new System.Drawing.Size(125, 32);
            btnDeleteStoreItem.Text = "Delete Store Item...";
            btnDeleteStoreItem.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
            btnDeleteStoreItem.Click += new System.EventHandler(btnDeleteStoreItemClick);
    
            btnClose = new Button();
            btnClose.Location = new Point(922, 366);
            btnClose.Size = new System.Drawing.Size(125, 32);
            btnClose.TabIndex = 4;
            btnClose.Text = "Close";
            btnClose.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
            btnClose.Click += new System.EventHandler(btnCloseClick);
    
            ClientSize = new System.Drawing.Size(1080, 410);
    
            Controls.Add(txtNumberOfItems);
            Controls.Add(lblNumberOfItems);
            Controls.Add(btnNewStoreItem);
            Controls.Add(btnEditStoreItem);
            Controls.Add(btnDeleteStoreItem);
            Controls.Add(btnClose);
            Controls.Add(lvwStoreItems);
    
            StartPosition = FormStartPosition.CenterScreen;
            Text = "Fun Department Store - Store Items";
            Load += new System.EventHandler(StoreItemsLoad);
        }
    
        private void ShowStoreItems()
        {
            XmlDocument xdStoreItems = new XmlDocument();
            string strStoreItemsFile = @"C:\Fun Department Store\StoreItems.xml";
    
            if (File.Exists(strStoreItemsFile))
            {
                lvwStoreItems.Items.Clear();
    
                using (FileStream fsStoreItems = new FileStream(strStoreItemsFile, FileMode.Open, FileAccess.Read))
                {
                    xdStoreItems.Load(fsStoreItems);
                    string itemName = "", itemSize = "", strDiscountRate = "";
                    string itemNumber = "", manufacturer = "", category = "", subCategory = "";
                    double unitPrice = 0.00, discountRate = 0.00, discountAmount = 0.00, markedPrice = 0.00;
    
                    XmlNodeList xnlStoreItems = xdStoreItems.GetElementsByTagName("ItemNumber");
    
                    int i = 1;
    
                    foreach (XmlNode xnStoreItem in xnlStoreItems)
                    {
                        itemNumber      = xnStoreItem.InnerText;
                        manufacturer    = xnStoreItem.NextSibling.InnerText;
                        category        = xnStoreItem.NextSibling.NextSibling.InnerText;
                        subCategory     = xnStoreItem.NextSibling.NextSibling.NextSibling.InnerText;
                        itemName        = xnStoreItem.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                        itemSize        = xnStoreItem.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                        unitPrice       = double.Parse(xnStoreItem.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText);
                        strDiscountRate = xnStoreItem.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
    
                        if ((string.IsNullOrEmpty(strDiscountRate)) || (double.Parse(strDiscountRate) <= 0.00))
                        {
                            discountRate = 0.00;
                            discountAmount = 0.00;
                            markedPrice = unitPrice;
                        }
                        else
                        {
                            discountRate = double.Parse(strDiscountRate) / 100.00;
                            discountAmount = unitPrice * discountRate;
                            markedPrice = unitPrice - discountAmount;
                        }
    
                        ListViewItem lviStoreItem = new ListViewItem(i.ToString());
                        lviStoreItem.SubItems.Add(itemNumber); // Item Number
                        lviStoreItem.SubItems.Add(manufacturer); // Manufacturer
                        lviStoreItem.SubItems.Add(category); // Category
                        lviStoreItem.SubItems.Add(subCategory); // Sub-Category
                        lviStoreItem.SubItems.Add(itemName); // Item Name
                        lviStoreItem.SubItems.Add(itemSize); // Item Size
                        lviStoreItem.SubItems.Add(unitPrice.ToString("F"));
    
                        if (discountRate == 0.00)
                        {
                            lviStoreItem.SubItems.Add(""); // Discount Rate
                            lviStoreItem.SubItems.Add(""); // Discount Amount
                            lviStoreItem.SubItems.Add(unitPrice.ToString("F")); // Marked Price
                        }
                        else
                        {
                            lviStoreItem.SubItems.Add((discountRate * 100).ToString() + "%");
                            lviStoreItem.SubItems.Add(discountAmount.ToString("F"));
                            lviStoreItem.SubItems.Add(markedPrice.ToString("F")); // Marked Price
                        }
                        
                        lvwStoreItems.Items.Add(lviStoreItem);
                        i++;
                    }
    
                    txtNumberOfItems.Text = xnlStoreItems.Count.ToString();
                }
            }
        }
    
        private void StoreItemsLoad(object sender, EventArgs e)
        {
            ShowStoreItems();
        }
    
        private void btnNewStoreItemClick(object sender, EventArgs e)
        {
            StoreItemNew sin = new StoreItemNew();
            sin.ShowDialog();
    
            ShowStoreItems();
        }
        
        private void btnEditStoreItemClick(object sender, EventArgs e)
        {
            StoreItemEditor sie = new StoreItemEditor();
            sie.ShowDialog();
    
            ShowStoreItems();
        }
    
        private void btnDeleteStoreItemClick(object sender, EventArgs e)
        {
            StoreItemDelete sid = new StoreItemDelete();
            sid.ShowDialog();
    
            ShowStoreItems();
        }
    
        private void btnCloseClick(object sender, EventArgs e)
        {
            Close();
        }
    }
  8. On the main menu, click File -> New
  9. When asked whether you want to save, click Save
  10. In the top combo box, make sure the FunDS1 folder is displaying, Set the Save As Type to All Files
  11. Set the File Name to Inventory.cs
  12. Save the file
 
   
 

The Point of Sale

A poinit of sale is the workstation where customers purchases are handled. Point of sales used to be a big issue. You had to purchase special machines and have a special setup. Although you still need setup, you can build the whole thing yourself (but we will not discuss all of that this time).

In our application, when a customer brings one or more items to check out, the cashier (or employee) will enter each item number in a text box and press enter. This will display information about the item, including the discount if any. The employee will do this for all items brought by the customer. The employee will also have the ability to remove an item. When all items have been entered, the employee can save the order and start another order or purchase.

Practical LearningPractical Learning: Creating a Point of Sale Tool

  1. In the empty document, type the following code:
    using System;
    using System.IO;
    using System.Xml;
    using System.Drawing;
    using System.Windows.Forms;
    
    public class NewShoppingSession : Form
    {
        private Label lblItemNumberSelected;
        private TextBox txtItemNumberSelected;
        private Label lblReceiptNumber;
        private TextBox txtReceiptNumber;
    
        private ColumnHeader colItemNumber;
        private ColumnHeader colItemName;
        private ColumnHeader colItemSize;
        private ColumnHeader colUnitPrice;
        private ColumnHeader colDiscountRate;
        private ColumnHeader colDiscountAmount;
        private ColumnHeader colSalePrice;
        private ListView lvwSoldItems;
    
        private TextBox txtEmployeeNumber;
        private Label lblEmployeeNumber;
        private DateTimePicker dtpSaleDate;
        private Label lblSaleTime;
        private TextBox txtAmountTendered;
        private Label lblTendered;
        private TextBox txtChange;
        private Label lblChange;
        private TextBox txtOrderTotal;
        private Label lblOrderTotal;
        private TextBox txtEmployeeName;
        private Button btnSubmit;
        private Button btnReset;
        private TextBox txtItemNumberRemove;
        private Label lblItemNumberToRemove;
        private Button btnRemoveItem;
        private Button btnClose;
    
        private Timer tmrDateTime;
    
        public NewShoppingSession()
        {
            InitializeComponent();
        }
    
        private void InitializeComponent()
        {
            // Label: Item Number to Add
            lblItemNumberSelected = new Label();
            lblItemNumberSelected.AutoSize = true;
            lblItemNumberSelected.Font = new System.Drawing.Font("Georgia", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            lblItemNumberSelected.Location = new Point(16, 22);
            lblItemNumberSelected.TabIndex = 2;
            lblItemNumberSelected.Text = "Item # to Add:";
    
            // Text Box: Item Number to Add
            txtItemNumberSelected = new TextBox();
            txtItemNumberSelected.Font = new System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            txtItemNumberSelected.Location = new Point(235, 18);
            txtItemNumberSelected.Size = new System.Drawing.Size(155, 41);
            txtItemNumberSelected.TabIndex = 3;
            txtItemNumberSelected.KeyUp += new KeyEventHandler(txtItemNumberKeyUp);
    
            // Label: Receipt Number
            lblReceiptNumber = new Label();
            lblReceiptNumber.AutoSize = true;
            lblReceiptNumber.Font = new System.Drawing.Font("Georgia", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            lblReceiptNumber.Location = new Point(1081, 22);
            lblReceiptNumber.TabIndex = 5;
            lblReceiptNumber.Text = "Receipt #:";
            Controls.Add(lblReceiptNumber);
    
            // Text Box: Receipt Number
            txtReceiptNumber = new TextBox();
            txtReceiptNumber.Font = new System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            txtReceiptNumber.Location = new Point(1260, 18);
            txtReceiptNumber.Size = new System.Drawing.Size(105, 41);
            txtReceiptNumber.TabIndex = 6;
            txtReceiptNumber.Text = "000000";
            Controls.Add(txtReceiptNumber);
    
            // Column: umn: Item Number
            colItemNumber = new ColumnHeader();
            colItemNumber.Text = "Item #";
            colItemNumber.Width = 100;
    
            // Column: umn: Item Name
            colItemName = new ColumnHeader();
            colItemName.Text = "Item Name/Description";
            colItemName.Width = 610;
    
            // Column: umn: Item Size
            colItemSize = new ColumnHeader();
            colItemSize.Text = "Size";
            colItemSize.Width = 150;
    
            // Column: umn: Unit Price
            colUnitPrice = new ColumnHeader();
            colUnitPrice.Text = "Unit Price";
            colUnitPrice.TextAlign = HorizontalAlignment.Right;
            colUnitPrice.Width = 140;
    
            // Column: umn: Discount Rate
            colDiscountRate = new ColumnHeader();
            colDiscountRate.Text = "Dscnt Rt";
            colDiscountRate.TextAlign = HorizontalAlignment.Right;
            colDiscountRate.Width = 120;
    
            // Column: umn: Discount Amount
            colDiscountAmount = new ColumnHeader();
            colDiscountAmount.Text = "Dscnt Amt";
            colDiscountAmount.TextAlign = HorizontalAlignment.Right;
            colDiscountAmount.Width = 140;
    
            // Column: umn: Sale Price
            colSalePrice = new ColumnHeader();
            colSalePrice.Text = "Sale Price";
            colSalePrice.TextAlign = HorizontalAlignment.Right;
            colSalePrice.Width = 130;
    
            // List View: Selected Items
            lvwSoldItems = new ListView();
            lvwSoldItems.Columns.AddRange(new ColumnHeader[]
            {
                colItemNumber, colItemName,	colItemSize, colUnitPrice,
                colDiscountRate, colDiscountAmount,	colSalePrice
            });
            lvwSoldItems.Font = new System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            lvwSoldItems.FullRowSelect = true;
            lvwSoldItems.GridLines = true;
            lvwSoldItems.Location = new Point(16, 71);
            lvwSoldItems.Size = new System.Drawing.Size(1424, 321);
            lvwSoldItems.TabIndex = 8;
            lvwSoldItems.View = View.Details;
    
            // Date/Time Picker: Sale Date
            dtpSaleDate = new DateTimePicker();
            dtpSaleDate.Font = new System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            dtpSaleDate.Location = new Point(16, 469);
            dtpSaleDate.Size = new System.Drawing.Size(402, 39);
            dtpSaleDate.TabIndex = 9;
    
            lblSaleTime = new Label();
            lblSaleTime.Font = new System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            lblSaleTime.Location = new Point(429, 469);
            lblSaleTime.Text = "Sale Time";
            lblSaleTime.Size = new System.Drawing.Size(177, 39);
            lblSaleTime.TabIndex = 11;
    
            // Label: Tendered
            lblTendered = new Label();
            lblTendered.AutoSize = true;
            lblTendered.Font = new System.Drawing.Font("Georgia", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            lblTendered.Location = new Point(1089, 449);
            lblTendered.TabIndex = 15;
            lblTendered.Text = "Tendered:";
    
            // Text Box: Amount Tendered
            txtAmountTendered = new TextBox();
            txtAmountTendered.Font = new System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            txtAmountTendered.Location = new Point(1278, 445);
            txtAmountTendered.Size = new System.Drawing.Size(130, 41);
            txtAmountTendered.TabIndex = 14;
            txtAmountTendered.Text = "0.00";
            txtAmountTendered.TextAlign = HorizontalAlignment.Right;
            txtAmountTendered.KeyUp += new KeyEventHandler(txtAmountTenderedKeyUp);
            txtAmountTendered.Leave += new EventHandler(txtAmountTenderedLeave);
    
            // Label: Item Number to Remove
            lblItemNumberToRemove = new Label();
            lblItemNumberToRemove.AutoSize = true;
            lblItemNumberToRemove.Font = new System.Drawing.Font("Georgia", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            lblItemNumberToRemove.Location = new Point(16, 416);
            lblItemNumberToRemove.TabIndex = 18;
            lblItemNumberToRemove.Text = "Item # to Remove:";
    
            // Text Box: Item Number to Remove
            txtItemNumberRemove = new TextBox();
            txtItemNumberRemove.Font = new System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            txtItemNumberRemove.Location = new Point(290, 412);
            txtItemNumberRemove.Size = new System.Drawing.Size(155, 41);
            txtItemNumberRemove.TabIndex = 19;
    
            // Button: Remove Item
            btnRemoveItem = new Button();
            btnRemoveItem.Font = new System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            btnRemoveItem.Location = new Point(451, 408);
            btnRemoveItem.Size = new System.Drawing.Size(155, 46);
            btnRemoveItem.TabIndex = 20;
            btnRemoveItem.Text = "Remove";
            btnRemoveItem.Click += new System.EventHandler(btnRemoveItemClick);
    
            // Label: Order Total
            lblOrderTotal = new Label();
            lblOrderTotal.AutoSize = true;
            lblOrderTotal.Font = new System.Drawing.Font("Georgia", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            lblOrderTotal.Location = new Point(1089, 401);
            lblOrderTotal.TabIndex = 12;
            lblOrderTotal.Text = "Order Total:";
            txtEmployeeNumber = new TextBox();
            txtEmployeeName = new TextBox();
            btnSubmit = new Button();
            btnReset = new Button();
            btnClose = new Button();
    
            // Text Box: Order Total
            txtOrderTotal = new TextBox();
            txtOrderTotal.Font = new System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            txtOrderTotal.Location = new Point(1278, 396);
            txtOrderTotal.Size = new System.Drawing.Size(130, 41);
            txtOrderTotal.TabIndex = 13;
            txtOrderTotal.Text = "0.00";
            txtOrderTotal.TextAlign = HorizontalAlignment.Right;
    
            // Label: Change
            lblChange = new Label();
            lblChange.AutoSize = true;
            lblChange.Font = new System.Drawing.Font("Georgia", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            lblChange.Location = new Point(1089, 496);
            lblChange.TabIndex = 16;
            lblChange.Text = "Change:";
    
            // Text Box: Change
            txtChange = new TextBox();
            txtChange.Font = new System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            txtChange.Location = new Point(1278, 492);
            txtChange.Size = new System.Drawing.Size(129, 41);
            txtChange.TabIndex = 17;
            txtChange.Text = "0.00";
            txtChange.TextAlign = HorizontalAlignment.Right;
    
            // Label: Employee Number
            lblEmployeeNumber = new Label();
            lblEmployeeNumber.AutoSize = true;
            lblEmployeeNumber.Font = new System.Drawing.Font("Georgia", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            lblEmployeeNumber.Location = new Point(10, 558);
            lblEmployeeNumber.TabIndex = 0;
            lblEmployeeNumber.Text = "Employee #:";
    
            // Text Box: Employee Number
            txtEmployeeNumber.Font = new System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            txtEmployeeNumber.Location = new Point(206, 554);
            txtEmployeeNumber.Size = new System.Drawing.Size(155, 41);
            txtEmployeeNumber.TabIndex = 1;
            txtEmployeeNumber.Leave += new System.EventHandler(txtEmployeeNumberLeave);
    
            // Text Box: Employee Name
            txtEmployeeName.Font = new System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            txtEmployeeName.Location = new Point(370, 554);
            txtEmployeeName.Size = new System.Drawing.Size(499, 41);
            txtEmployeeName.TabIndex = 7;
    
            // Button: Reset
            btnReset.Font = new System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            btnReset.Location = new Point(923, 551);
            btnReset.Size = new System.Drawing.Size(168, 46);
            btnReset.TabIndex = 10;
            btnReset.Text = "Reset";
            btnReset.Click += new System.EventHandler(btnResetClick);
    
            // Button: Submit
            btnSubmit.Font = new System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            btnSubmit.Location = new Point(1097, 551);
            btnSubmit.Size = new System.Drawing.Size(177, 46);
            btnSubmit.TabIndex = 4;
            btnSubmit.Text = "Submit";
            btnSubmit.Click += new System.EventHandler(btnSubmitClick);
    
            // Button: Close
            btnClose.Font = new System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            btnClose.Location = new Point(1281, 551);
            btnClose.Size = new System.Drawing.Size(129, 46);
            btnClose.TabIndex = 21;
            btnClose.Text = "Close";
            btnClose.Click += new System.EventHandler(btnCloseClick);
    
            tmrDateTime = new Timer();
            tmrDateTime.Enabled = true;
            tmrDateTime.Tick += new EventHandler(tmrDateTimeTick);
    
            ClientSize = new System.Drawing.Size(1454, 611);
            Controls.Add(btnClose);
            Controls.Add(btnRemoveItem);
            Controls.Add(txtItemNumberRemove);
            Controls.Add(lblItemNumberToRemove);
            Controls.Add(btnReset);
            Controls.Add(btnSubmit);
            Controls.Add(txtEmployeeName);
            Controls.Add(txtOrderTotal);
            Controls.Add(lblOrderTotal);
            Controls.Add(txtChange);
            Controls.Add(lblChange);
            Controls.Add(txtAmountTendered);
            Controls.Add(lblTendered);
            Controls.Add(lblSaleTime);
            Controls.Add(dtpSaleDate);
            Controls.Add(txtEmployeeNumber);
            Controls.Add(lblEmployeeNumber);
            Controls.Add(lvwSoldItems);
            Controls.Add(txtItemNumberSelected);
            Controls.Add(lblItemNumberSelected);
            MaximizeBox = false;
            MinimizeBox = false;
            StartPosition = FormStartPosition.CenterScreen;
            Text = "Fun Department Store - New Shopping Session";
            Load += new System.EventHandler(ShoppingSessionLoad);
        }
    
        private void txtItemNumberKeyUp(object sender, KeyEventArgs e)
        {
            bool itemFound = false;
            double dblTotal = 0.00;
            XmlDocument xdStoreItems = new XmlDocument();
            string strStoreItemsFile = @"C:\Fun Department Store\StoreItems.xml";
    
            if (e.KeyCode == Keys.Enter)
            {
                if (string.IsNullOrEmpty(txtItemNumberSelected.Text))
                {
                    MessageBox.Show("You must enter an item number.",
                                    "FunDS - Fun Department Store",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    if (File.Exists(strStoreItemsFile))
                    {
                        using (FileStream fsStoreItems = new FileStream(strStoreItemsFile, FileMode.Open, FileAccess.Read))
                        {
                            xdStoreItems.Load(fsStoreItems);
                            string itemNumber, itemName, itemSize, strDiscountRate;
                            double unitPrice, discountRate, discountAmount, salePrice;
    
                            XmlNodeList xnlStoreItems = xdStoreItems.GetElementsByTagName("ItemNumber");
    
                            foreach (XmlNode xnStoreItem in xnlStoreItems)
                            {
                                if (xnStoreItem.InnerText == txtItemNumberSelected.Text)
                                {
                                    itemFound = true;
    
                                    itemNumber = xnStoreItem.InnerText;
                                    itemName = xnStoreItem.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                    itemSize = xnStoreItem.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                    unitPrice = double.Parse(xnStoreItem.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText);
                                    strDiscountRate = xnStoreItem.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
    
                                    if ((string.IsNullOrEmpty(strDiscountRate)) || (double.Parse(strDiscountRate) <= 0.00))
                                    {
                                        discountRate = 0.00;
                                        discountAmount = 0.00;
                                        salePrice = unitPrice;
                                    }
                                    else
                                    {
                                        discountRate = double.Parse(strDiscountRate) / 100.00;
                                        discountAmount = unitPrice * discountRate;
                                        salePrice = unitPrice - discountAmount;
                                    }
    
                                    ListViewItem lviStoreItem = new ListViewItem(itemNumber); // Item Number
                                    lviStoreItem.SubItems.Add(itemName); // Item Name
                                    lviStoreItem.SubItems.Add(itemSize); // Item Size
                                    lviStoreItem.SubItems.Add(unitPrice.ToString("F"));
    
                                    if (discountRate == 0.00)
                                    {
                                        lviStoreItem.SubItems.Add(""); // Discount Rate
                                        lviStoreItem.SubItems.Add(""); // Discount Amount
                                        lviStoreItem.SubItems.Add(unitPrice.ToString("F")); // Unit Price
                                    }
                                    else
                                    {
                                        lviStoreItem.SubItems.Add((discountRate * 100).ToString() + "%");
                                        lviStoreItem.SubItems.Add(discountAmount.ToString("F"));
                                        lviStoreItem.SubItems.Add(salePrice.ToString("F"));
                                    }
    
                                    lvwSoldItems.Items.Add(lviStoreItem);
                                }
                            }
    
                            txtItemNumberSelected.Text = "";
    
                            if (itemFound == false)
                            {
                                MessageBox.Show("There is no item with that item number.",
                                                "FunDS - Fun Department Store",
                                                MessageBoxButtons.OK,
                                                MessageBoxIcon.Information);
                            }
                        }
    
                        if (lvwSoldItems.Items.Count > 0)
                        {
                            foreach (ListViewItem lviStoreItem in lvwSoldItems.Items)
                                dblTotal += double.Parse(lviStoreItem.SubItems[6].Text);
    
                            txtOrderTotal.Text = dblTotal.ToString("F");
                        }
                    }
                }
            }
        }
    
        private void ShoppingSessionLoad(object sender, EventArgs e)
        {
            btnResetClick(sender, e);
        }
    
        private void txtEmployeeNumberLeave(object sender, EventArgs e)
        {
            bool employeeFound = false;
            XmlDocument xdEmployees = new XmlDocument();
            string strEmployeesFile = @"C:\Fun Department Store\Employees.xml";
    
            if (string.IsNullOrEmpty(txtEmployeeNumber.Text))
            {
                return;
            }
            else
            {
                if (File.Exists(strEmployeesFile))
                {
                    using (FileStream fsEmployees = new FileStream(strEmployeesFile, FileMode.Open, FileAccess.Read))
                    {
                        xdEmployees.Load(fsEmployees);
    
                        XmlNodeList xnlEmployees = xdEmployees.GetElementsByTagName("EmployeeNumber");
    
                        foreach (XmlNode xnEmployee in xnlEmployees)
                        {
                            if (txtEmployeeNumber.Text == xnEmployee.InnerText)
                            {
                                employeeFound = true;
                                txtEmployeeName.Text = string.Concat(xnEmployee.NextSibling.NextSibling.InnerText, ", ", xnEmployee.NextSibling.InnerText);
                            }
                        }
    
                        if (employeeFound == false)
                        {
                            MessageBox.Show("There is no employee with that number.",
                                    "FunDS - Fun Department Store",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                        }
                    }
                }
            }
        }
    
        private void txtAmountTenderedKeyUp(object sender, KeyEventArgs e)
        {
            double orderTotal = 0.00;
            double amountTendered = 0.00;
            double change = 0.00;
    
            if ((e.KeyCode == Keys.Tab) || (e.KeyCode == Keys.Enter))
            {
                if (string.IsNullOrEmpty(txtOrderTotal.Text))
                {
                    return;
                }
                else if (string.IsNullOrEmpty(txtAmountTendered.Text))
                {
                    return;
                }
                else
                {
                    orderTotal = double.Parse(txtOrderTotal.Text);
                    amountTendered = double.Parse(txtAmountTendered.Text);
                    change = amountTendered - orderTotal;
    
                    txtChange.Text = change.ToString("F");
                }
            }
        }
    
        private void txtAmountTenderedLeave(object sender, EventArgs e)
        {
            double orderTotal = 0.00;
            double amountTendered = 0.00;
            double change = 0.00;
    
            if (string.IsNullOrEmpty(txtOrderTotal.Text))
            {
                return;
            }
            else if (string.IsNullOrEmpty(txtAmountTendered.Text))
            {
                return;
            }
            else
            {
                orderTotal = double.Parse(txtOrderTotal.Text);
                amountTendered = double.Parse(txtAmountTendered.Text);
                change = amountTendered - orderTotal;
    
                txtChange.Text = change.ToString("F");
            }
        }
    
        private void btnRemoveItemClick(object sender, EventArgs e)
        {
            double dblTotal = 0.00;
            bool itemFound = false;
    
            if (lvwSoldItems.Items.Count == 0)
            {
                MessageBox.Show("The list view is empty.",
                                "FunDS", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else if (string.IsNullOrEmpty(txtItemNumberRemove.Text))
            {
                MessageBox.Show("You must enter an item number and that exists in the list view.",
                                "FunDS", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                foreach (ListViewItem lviStoreItem in lvwSoldItems.Items)
                {
                    if (lviStoreItem.SubItems[0].Text.Equals(txtItemNumberRemove.Text))
                    {
                        itemFound = true;
                        lvwSoldItems.Items.Remove(lviStoreItem);
                    }
                }
    
                if (itemFound == false)
                {
                    MessageBox.Show("That item number is not in the list view.",
                                    "FunDS", MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                    return;
                }
    
                if (lvwSoldItems.Items.Count > 0)
                {
                    foreach (ListViewItem lviStoreItem in lvwSoldItems.Items)
                        dblTotal += double.Parse(lviStoreItem.SubItems[6].Text);
    
                    txtOrderTotal.Text = dblTotal.ToString("F");
                    txtAmountTendered.Text = "0.00";
                    txtChange.Text = "0.00";
                }
                else
                {
                    txtOrderTotal.Text = "0.00";
                    txtAmountTendered.Text = "0.00";
                    txtChange.Text = "0.00";
                }
    
                txtItemNumberRemove.Text = "";
            }
        }
    
        private void btnResetClick(object sender, EventArgs e)
        {
            int iReceiptNumber = 100000;
            XmlDocument xdStoreSales = new XmlDocument();
            string strStoreSalesFile = @"C:\Fun Department Store\StoreSales.xml";
    
            if (File.Exists(strStoreSalesFile))
            {
                using (FileStream fsStoreSales = new FileStream(strStoreSalesFile, FileMode.Open, FileAccess.Read))
                {
                    xdStoreSales.Load(fsStoreSales);
    
                    XmlNodeList xnlStoreSales = xdStoreSales.GetElementsByTagName("ReceiptNumber");
    
                    foreach (XmlNode xnStoreSale in xnlStoreSales)
                    {
                        iReceiptNumber = int.Parse(xnStoreSale.InnerText);
                    }
                }
            }
    
            txtReceiptNumber.Text = (iReceiptNumber + 1).ToString();
            txtItemNumberSelected.Text = "";
            lvwSoldItems.Items.Clear();
            txtItemNumberRemove.Text = "";
            dtpSaleDate.Value = DateTime.Now;
            lblSaleTime.Text = DateTime.Now.ToString();
            txtEmployeeNumber.Text = ""; ;
            txtEmployeeName.Text = "";
            txtOrderTotal.Text = "0.00";
            txtAmountTendered.Text = "0.00";
            txtChange.Text = "0.00";
        }
    
        private void btnSubmitClick(object sender, EventArgs e)
        {
            int iSoldItemID = 0;
            XmlDocument xdSoldItems = new XmlDocument();
            XmlDocument xdStoreSales = new XmlDocument();
            XmlDocument xdStoreItems = new XmlDocument();
            string strSoldItemsFile = @"C:\Fun Department Store\SoldItems.xml";
            string strStoreSalesFile = @"C:\Fun Department Store\StoreSales.xml";
            string strStoreItemsFile = @"C:\Fun Department Store\StoreItems1.xml";
    
            if (lvwSoldItems.Items.Count == 0)
            {
                MessageBox.Show("There is no customer order to save.",
                                "FunDS", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
    
            if (!File.Exists(strStoreSalesFile))
            {
                using (FileStream fsStoreSales = new FileStream(strStoreSalesFile, FileMode.Create, FileAccess.Write))
                {
                    xdStoreSales.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                                         "<StoreSales></StoreSales>");
                    xdStoreSales.Save(fsStoreSales);
                }
            }
    
            using (FileStream fsStoreSales = new FileStream(strStoreSalesFile, FileMode.Open, FileAccess.Read))
            {
                xdStoreSales.Load(fsStoreSales);
            }
    
            using (FileStream fsStoreSales = new FileStream(strStoreSalesFile, FileMode.Create, FileAccess.Write))
            {
                XmlElement xeStoreSale = xdStoreSales.CreateElement("StoreSale");
                xeStoreSale.InnerXml = "<ReceiptNumber>" + txtReceiptNumber.Text + "</ReceiptNumber>" +
                                       "<EmployeeNumber>" + txtEmployeeNumber.Text + "</EmployeeNumber>" +
                                       "<SaleDate>" + dtpSaleDate.Value.ToShortDateString() + "</SaleDate>" +
                                       "<SaleTime>" + lblSaleTime.Text + "</SaleTime>" +
                                       "<OrderTotal>" + txtOrderTotal.Text + "</OrderTotal>" +
                                       "<AmountTendered>" + txtAmountTendered.Text + "</AmountTendered>" +
                                       "<Change>" + txtChange.Text + "</Change>";
                xdStoreSales.DocumentElement.AppendChild(xeStoreSale);
    
                xdStoreSales.Save(fsStoreSales);
            }
    
            if (!File.Exists(strSoldItemsFile))
            {
                using (FileStream fsSoldItems = new FileStream(strSoldItemsFile, FileMode.Create, FileAccess.Write))
                {
                    xdSoldItems.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                                        "<SoldItems></SoldItems>");
                    xdSoldItems.Save(fsSoldItems);
                }
            }
    
            using (FileStream fsSoldItems = new FileStream(strSoldItemsFile, FileMode.Open, FileAccess.Read))
            {
                xdSoldItems.Load(fsSoldItems);
                XmlNodeList xnlSoldItems = xdSoldItems.GetElementsByTagName("SoldItemID");
    
                foreach (XmlNode xnSoldItem in xnlSoldItems)
                    iSoldItemID = int.Parse(xnSoldItem.InnerText);
            }
    
            iSoldItemID = iSoldItemID + 1;
    
            using (FileStream fsSoldItems = new FileStream(strSoldItemsFile, FileMode.Create, FileAccess.Write))
            {
                foreach (ListViewItem lviSoldItem in lvwSoldItems.Items)
                {
                    XmlElement xeSoldItem = xdSoldItems.CreateElement("SoldItem");
                    xeSoldItem.InnerXml = "<SoldItemID>" + iSoldItemID.ToString() + "</SoldItemID>" +
                                          "<ReceiptNumber>" + txtReceiptNumber.Text + "</ReceiptNumber>" +
                                          "<ItemNumber>" + lviSoldItem.SubItems[0].Text + "</ItemNumber>" +
                                          "<ItemName>" + lviSoldItem.SubItems[1].Text + "</ItemName>" +
                                          "<ItemSize>" + lviSoldItem.SubItems[2].Text + "</ItemSize>" +
                                          "<UnitPrice>" + lviSoldItem.SubItems[3].Text + "</UnitPrice>" +
                                          "<DiscountRate>" + lviSoldItem.SubItems[4].Text + "</DiscountRate>" +
                                          "<DiscountAmount>" + lviSoldItem.SubItems[5].Text + "</DiscountAmount>" +
                                          "<SalePrice>" + lviSoldItem.SubItems[6].Text + "</SalePrice>";
                    xdSoldItems.DocumentElement.AppendChild(xeSoldItem);
                    iSoldItemID++;
                }
    
                xdSoldItems.Save(fsSoldItems);
    
                MessageBox.Show("The customer's order has been saved.",
                                "FunDS", MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
            }
    
            if (File.Exists(strStoreItemsFile))
            {
                using (FileStream fsStoreItems = new FileStream(strStoreItemsFile, FileMode.Open, FileAccess.Read))
                {
                    xdStoreItems.Load(fsStoreItems);
    
                    XmlNodeList xnlStoreItems = xdStoreItems.GetElementsByTagName("ItemNumber");
    
                    // Remove the items of the list view from the StoreItems table
                    foreach (ListViewItem lviStoreItem in lvwSoldItems.Items)
                    {
                        foreach (XmlNode xnStoreItem in xnlStoreItems)
                        {
                            if (xnStoreItem.InnerText == lviStoreItem.SubItems[0].Text)
                                xdStoreItems.DocumentElement.RemoveChild(xnStoreItem.ParentNode);
                        }
                    }
                }
    
                using (FileStream fsStoreItems = new FileStream(strStoreItemsFile, FileMode.Create, FileAccess.Write))
                {
                    xdStoreItems.Save(fsStoreItems);
                }
            }
    
            btnResetClick(sender, e);
        }
    
        private void btnCloseClick(object sender, EventArgs e)
        {
            Close();
        }
    
        private void tmrDateTimeTick(object sender, EventArgs e)
        {
            dtpSaleDate.Value = DateTime.Today;
            lblSaleTime.Text = DateTime.Now.ToLongTimeString();
        }
    }
  2. On the main menu, click File -> New
  3. When asked whether you want to save, click Save
  4. In the top combo box, make sure the FunDS1 folder is displaying, Set the Save As Type to All Files
  5. Set the File Name to PointOfSale.cs
  6. Click Save

Reviewing a Shopping Session

Some time to time, the company management must review store sales. Also, a customer may come with a complaint about anything. The first thing to do is to review a customer order. To handle this, we will create a form in which a user can enter a receipt number and click a button to locate its record. If the purchase is found, the form will display (that's all we will do at this time; otherwise, there are many actions that can be taken such as deleting an items from the receipt (and re-imbursing the customer) and putting the information about the returned item to another list, etc).

Practical LearningPractical Learning: Reviewing a Shopping Session

  1. In the empty document, type the following:
    using System;
    using System.IO;
    using System.Xml;
    using System.Drawing;
    using System.Windows.Forms;
    
    public class ShoppingSessionReview : Form
    {
        private Label lblItemNumberSelected;
        private TextBox txtItemNumberSelected;
        private Label lblReceiptNumber;
        private TextBox txtReceiptNumber;
        private Button btnFind;
        private ColumnHeader colItemNumber;
        private ColumnHeader colItemName;
        private ColumnHeader colItemSize;
        private ColumnHeader colUnitPrice;
        private ColumnHeader colDiscountRate;
        private ColumnHeader colDiscountAmount;
        private ColumnHeader colSalePrice;
        private ListView lvwSoldItems;
    
        private TextBox txtEmployeeNumber;
        private Label lblEmployeeNumber;
        private DateTimePicker dtpSaleDate;
        private Label lblSaleTime;
        private TextBox txtAmountTendered;
        private Label lblTendered;
        private TextBox txtChange;
        private Label lblChange;
        private TextBox txtOrderTotal;
        private Label lblOrderTotal;
        private TextBox txtEmployeeName;
        private Button btnSubmit;
        private TextBox txtItemNumberRemove;
        private Label lblItemNumberToRemove;
        private Button btnRemoveItem;
        private Button btnClose;
    
        private Timer tmrDateTime;
    
        public ShoppingSessionReview()
        {
            InitializeComponent();
        }
    
        private void InitializeComponent()
        {
            // Label: Item Number to Add
            lblItemNumberSelected = new Label();
            lblItemNumberSelected.AutoSize = true;
            lblItemNumberSelected.Font = new System.Drawing.Font("Georgia", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            lblItemNumberSelected.Location = new Point(16, 22);
            lblItemNumberSelected.TabIndex = 20;
            lblItemNumberSelected.Text = "Item # to Add:";
    
            // Text Box: Item Number to Add
            txtItemNumberSelected = new TextBox();
            txtItemNumberSelected.Font = new System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            txtItemNumberSelected.Location = new Point(235, 18);
            txtItemNumberSelected.Size = new System.Drawing.Size(155, 41);
            txtItemNumberSelected.TabIndex = 21;
            txtItemNumberSelected.KeyUp += new KeyEventHandler(txtItemNumberKeyUp);
    
            // Label: Receipt Number
            lblReceiptNumber = new Label();
            lblReceiptNumber.AutoSize = true;
            lblReceiptNumber.Font = new System.Drawing.Font("Georgia", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            lblReceiptNumber.Location = new Point(881, 22);
            lblReceiptNumber.TabIndex = 0;
            lblReceiptNumber.Text = "Receipt #:";
    
            // Text Box: Receipt Number
            txtReceiptNumber = new TextBox();
            txtReceiptNumber.Font = new System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            txtReceiptNumber.Location = new Point(1060, 18);
            txtReceiptNumber.Size = new System.Drawing.Size(105, 41);
            txtReceiptNumber.TabIndex = 1;
    
            // Button: Find
            btnFind = new Button();
            btnFind.Font = new System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            btnFind.Location = new Point(1178, 16);
            btnFind.Size = new System.Drawing.Size(168, 46);
            btnFind.TabIndex = 2;
            btnFind.Text = "Find";
            btnFind.Click += new System.EventHandler(btnFindClick);
            Controls.Add(btnFind);
    
            // Column: umn: Item Number
            colItemNumber = new ColumnHeader();
            colItemNumber.Text = "Item #";
            colItemNumber.Width = 100;
    
            // Column: umn: Item Name
            colItemName = new ColumnHeader();
            colItemName.Text = "Item Name/Description";
            colItemName.Width = 610;
    
            // Column: umn: Item Size
            colItemSize = new ColumnHeader();
            colItemSize.Text = "Size";
            colItemSize.Width = 150;
    
            // Column: umn: Unit Price
            colUnitPrice = new ColumnHeader();
            colUnitPrice.Text = "Unit Price";
            colUnitPrice.TextAlign = HorizontalAlignment.Right;
            colUnitPrice.Width = 140;
    
            // Column: umn: Discount Rate
            colDiscountRate = new ColumnHeader();
            colDiscountRate.Text = "Dscnt Rt";
            colDiscountRate.TextAlign = HorizontalAlignment.Right;
            colDiscountRate.Width = 120;
    
            // Column: umn: Discount Amount
            colDiscountAmount = new ColumnHeader();
            colDiscountAmount.Text = "Dscnt Amt";
            colDiscountAmount.TextAlign = HorizontalAlignment.Right;
            colDiscountAmount.Width = 140;
    
            // Column: umn: Sale Price
            colSalePrice = new ColumnHeader();
            colSalePrice.Text = "Sale Price";
            colSalePrice.TextAlign = HorizontalAlignment.Right;
            colSalePrice.Width = 130;
    
            // List View: Selected Items
            lvwSoldItems = new ListView();
            lvwSoldItems.Columns.AddRange(new ColumnHeader[]
            {
                colItemNumber, colItemName,	colItemSize, colUnitPrice,
                colDiscountRate, colDiscountAmount,	colSalePrice
            });
            lvwSoldItems.Font = new System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            lvwSoldItems.FullRowSelect = true;
            lvwSoldItems.GridLines = true;
            lvwSoldItems.Location = new Point(16, 71);
            lvwSoldItems.Size = new System.Drawing.Size(1424, 321);
            lvwSoldItems.TabIndex = 8;
            lvwSoldItems.View = View.Details;
    
            // Date/Time Picker: Sale Date
            dtpSaleDate = new DateTimePicker();
            dtpSaleDate.Font = new System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            dtpSaleDate.Location = new Point(16, 469);
            dtpSaleDate.Size = new System.Drawing.Size(402, 39);
            dtpSaleDate.TabIndex = 9;
    
            lblSaleTime = new Label();
            lblSaleTime.Font = new System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            lblSaleTime.Location = new Point(429, 469);
            lblSaleTime.Text = "Sale Time";
            lblSaleTime.Size = new System.Drawing.Size(177, 39);
            lblSaleTime.TabIndex = 11;
    
            // Label: Tendered
            lblTendered = new Label();
            lblTendered.AutoSize = true;
            lblTendered.Font = new System.Drawing.Font("Georgia", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            lblTendered.Location = new Point(1089, 449);
            lblTendered.TabIndex = 15;
            lblTendered.Text = "Tendered:";
    
            // Text Box: Amount Tendered
            txtAmountTendered = new TextBox();
            txtAmountTendered.Font = new System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            txtAmountTendered.Location = new Point(1278, 445);
            txtAmountTendered.Size = new System.Drawing.Size(130, 41);
            txtAmountTendered.TabIndex = 14;
            txtAmountTendered.Text = "0.00";
            txtAmountTendered.TextAlign = HorizontalAlignment.Right;
            txtAmountTendered.KeyUp += new KeyEventHandler(txtAmountTenderedKeyUp);
            txtAmountTendered.Leave += new EventHandler(txtAmountTenderedLeave);
    
            // Label: Item Number to Remove
            lblItemNumberToRemove = new Label();
            lblItemNumberToRemove.AutoSize = true;
            lblItemNumberToRemove.Font = new System.Drawing.Font("Georgia", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            lblItemNumberToRemove.Location = new Point(16, 416);
            lblItemNumberToRemove.TabIndex = 18;
            lblItemNumberToRemove.Text = "Item # to Remove:";
    
            // Text Box: Item Number to Remove
            txtItemNumberRemove = new TextBox();
            txtItemNumberRemove.Font = new System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            txtItemNumberRemove.Location = new Point(290, 412);
            txtItemNumberRemove.Size = new System.Drawing.Size(155, 41);
            txtItemNumberRemove.TabIndex = 19;
    
            // Button: Remove Item
            btnRemoveItem = new Button();
            btnRemoveItem.Font = new System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            btnRemoveItem.Location = new Point(451, 408);
            btnRemoveItem.Size = new System.Drawing.Size(155, 46);
            btnRemoveItem.TabIndex = 20;
            btnRemoveItem.Text = "Remove";
            btnRemoveItem.Click += new System.EventHandler(btnRemoveItemClick);
    
            // Label: Order Total
            lblOrderTotal = new Label();
            lblOrderTotal.AutoSize = true;
            lblOrderTotal.Font = new System.Drawing.Font("Georgia", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            lblOrderTotal.Location = new Point(1089, 401);
            lblOrderTotal.TabIndex = 12;
            lblOrderTotal.Text = "Order Total:";
    
            // Text Box: Order Total
            txtOrderTotal = new TextBox();
            txtOrderTotal.Font = new System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            txtOrderTotal.Location = new Point(1278, 396);
            txtOrderTotal.Size = new System.Drawing.Size(130, 41);
            txtOrderTotal.TabIndex = 13;
            txtOrderTotal.Text = "0.00";
            txtOrderTotal.TextAlign = HorizontalAlignment.Right;
    
            // Label: Change
            lblChange = new Label();
            lblChange.AutoSize = true;
            lblChange.Font = new System.Drawing.Font("Georgia", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            lblChange.Location = new Point(1089, 496);
            lblChange.TabIndex = 16;
            lblChange.Text = "Change:";
    
            // Text Box: Change
            txtChange = new TextBox();
            txtChange.Font = new System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            txtChange.Location = new Point(1278, 492);
            txtChange.Size = new System.Drawing.Size(129, 41);
            txtChange.TabIndex = 17;
            txtChange.Text = "0.00";
            txtChange.TextAlign = HorizontalAlignment.Right;
    
            // Label: Employee Number
            lblEmployeeNumber = new Label();
            lblEmployeeNumber.AutoSize = true;
            lblEmployeeNumber.Font = new System.Drawing.Font("Georgia", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            lblEmployeeNumber.Location = new Point(10, 558);
            lblEmployeeNumber.TabIndex = 0;
            lblEmployeeNumber.Text = "Employee #:";
    
            // Text Box: Employee Number
            txtEmployeeNumber = new TextBox();
            txtEmployeeNumber.Font = new System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            txtEmployeeNumber.Location = new Point(206, 554);
            txtEmployeeNumber.Size = new System.Drawing.Size(155, 41);
            txtEmployeeNumber.TabIndex = 1;
            txtEmployeeNumber.Leave += new System.EventHandler(txtEmployeeNumberLeave);
    
            // Text Box: Employee Name
            txtEmployeeName = new TextBox();
            txtEmployeeName.Font = new System.Drawing.Font("Times New Roman", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            txtEmployeeName.Location = new Point(370, 554);
            txtEmployeeName.Size = new System.Drawing.Size(499, 41);
            txtEmployeeName.TabIndex = 7;
    
            // Button: Submit
            btnSubmit = new Button();
            btnSubmit.Font = new System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            btnSubmit.Location = new Point(1097, 551);
            btnSubmit.Size = new System.Drawing.Size(177, 46);
            btnSubmit.TabIndex = 3;
            btnSubmit.Text = "Submit";
            btnSubmit.Click += new System.EventHandler(btnSubmitClick);
            Controls.Add(btnSubmit);
    
            // Button: Close
            btnClose = new Button();
            btnClose.Font = new System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            btnClose.Location = new Point(1281, 551);
            btnClose.Size = new System.Drawing.Size(129, 46);
            btnClose.TabIndex = 4;
            btnClose.Text = "Close";
            btnClose.Click += new System.EventHandler(btnCloseClick);
            Controls.Add(btnClose);
    
            tmrDateTime = new Timer();
            tmrDateTime.Enabled = false;
            tmrDateTime.Tick += new EventHandler(tmrDateTimeTick);
    
            ClientSize = new System.Drawing.Size(1454, 611);
            Controls.Add(txtItemNumberSelected);
            Controls.Add(lblItemNumberSelected);
            Controls.Add(txtReceiptNumber);
            Controls.Add(lblReceiptNumber);
            Controls.Add(btnRemoveItem);
            Controls.Add(txtItemNumberRemove);
            Controls.Add(lblItemNumberToRemove);
            Controls.Add(txtEmployeeName);
            Controls.Add(txtOrderTotal);
            Controls.Add(lblOrderTotal);
            Controls.Add(txtChange);
            Controls.Add(lblChange);
            Controls.Add(txtAmountTendered);
            Controls.Add(lblTendered);
            Controls.Add(lblSaleTime);
            Controls.Add(dtpSaleDate);
            Controls.Add(txtEmployeeNumber);
            Controls.Add(lblEmployeeNumber);
            Controls.Add(lvwSoldItems);
            MaximizeBox = false;
            MinimizeBox = false;
            StartPosition = FormStartPosition.CenterScreen;
            Text = "FunDS - Shopping Session";
            Load += new System.EventHandler(ShoppingSessionLoad);
        }
    
        private void btnFindClick(object sender, EventArgs e)
        {
            bool receiptFound = false;
            XmlDocument xdSoldItems = new XmlDocument();
            XmlDocument xdStoreSales = new XmlDocument();
            string strSoldItemsFile = @"C:\Fun Department Store\SoldItems.xml";
            string strStoreSalesFile = @"C:\Fun Department Store\StoreSales.xml";
    
            if (string.IsNullOrEmpty(txtReceiptNumber.Text))
            {
                MessageBox.Show("You must enter a receipt number.",
                                "FunDS - Fun Department Store",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
                return;
            }
    
            if (File.Exists(strStoreSalesFile))
            {
                using (FileStream fsStoreSales = new FileStream(strStoreSalesFile, FileMode.Open, FileAccess.Read))
                {
                    xdStoreSales.Load(fsStoreSales);
    
                    XmlNodeList xnlStoreSales = xdStoreSales.GetElementsByTagName("ReceiptNumber");
    
                    foreach (XmlNode xnStoreSale in xnlStoreSales)
                    {
                        if (xnStoreSale.InnerText == txtReceiptNumber.Text)
                        {
                            receiptFound = true;
    
                            txtEmployeeNumber.Text = xnStoreSale.NextSibling.InnerText;
                            dtpSaleDate.Value = DateTime.Parse(xnStoreSale.NextSibling.NextSibling.InnerText);
                            lblSaleTime.Text = xnStoreSale.NextSibling.NextSibling.NextSibling.InnerText;
                            txtOrderTotal.Text = xnStoreSale.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                            txtAmountTendered.Text = xnStoreSale.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                            txtChange.Text = xnStoreSale.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
    
                            txtEmployeeNumberLeave(sender, e);
                        }
                    }
                }
            }
    
            if (File.Exists(strSoldItemsFile))
            {
                using (FileStream fsSoldItems = new FileStream(strSoldItemsFile, FileMode.Open, FileAccess.Read))
                {
                    xdSoldItems.Load(fsSoldItems);
                    XmlNodeList xnlSoldItems = xdSoldItems.GetElementsByTagName("ReceiptNumber");
    
                    lvwSoldItems.Items.Clear();
    
                    foreach (XmlNode xnSoldItem in xnlSoldItems)
                    {
                        if (xnSoldItem.InnerText == txtReceiptNumber.Text)
                        {
                            ListViewItem lviStoreItem = new ListViewItem(xnSoldItem.NextSibling.InnerText); // Item Number
                            lviStoreItem.SubItems.Add(xnSoldItem.NextSibling.NextSibling.InnerText); // Item Name
                            lviStoreItem.SubItems.Add(xnSoldItem.NextSibling.NextSibling.NextSibling.InnerText); // Item Size
                            lviStoreItem.SubItems.Add(xnSoldItem.NextSibling.NextSibling.NextSibling.NextSibling.InnerText); // Unit Price
                            lviStoreItem.SubItems.Add(xnSoldItem.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText); // Discount Rate
                            lviStoreItem.SubItems.Add(xnSoldItem.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText); // Discount Amount
                            lviStoreItem.SubItems.Add(xnSoldItem.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText); // Sale Price
    
                            lvwSoldItems.Items.Add(lviStoreItem);
                        }
                    }
                }
            }
    
            if (receiptFound == false)
            {
                MessageBox.Show("There is no sale order with that receipt number.",
                                "FunDS - Fun Department Store",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
                return;
            }
        }
    
        private void ResetForm()
        {
            txtReceiptNumber.Text = "";
            txtItemNumberSelected.Text = "";
            lvwSoldItems.Items.Clear();
            txtItemNumberRemove.Text = "";
            dtpSaleDate.Value = DateTime.Now;
            lblSaleTime.Text = DateTime.Now.ToString();
            txtEmployeeNumber.Text = ""; ;
            txtEmployeeName.Text = "";
            txtOrderTotal.Text = "0.00";
            txtAmountTendered.Text = "0.00";
            txtChange.Text = "0.00";
        }
    
        private void txtItemNumberKeyUp(object sender, KeyEventArgs e)
        {
            bool itemFound = false;
            double dblTotal = 0.00;
            XmlDocument xdStoreItems = new XmlDocument();
            string strStoreItemsFile = @"C:\Fun Department Store\StoreItems.xml";
    
            if (e.KeyCode == Keys.Enter)
            {
                if (string.IsNullOrEmpty(txtItemNumberSelected.Text))
                {
                    MessageBox.Show("You must enter an item number.",
                                    "FunDS - Fun Department Store",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    if (File.Exists(strStoreItemsFile))
                    {
                        using (FileStream fsStoreItems = new FileStream(strStoreItemsFile, FileMode.Open, FileAccess.Read))
                        {
                            xdStoreItems.Load(fsStoreItems);
                            string itemNumber, itemName, itemSize, strDiscountRate;
                            double unitPrice, discountRate, discountAmount, salePrice;
    
                            XmlNodeList xnlStoreItems = xdStoreItems.GetElementsByTagName("ItemNumber");
    
                            foreach (XmlNode xnStoreItem in xnlStoreItems)
                            {
                                if (xnStoreItem.InnerText == txtItemNumberSelected.Text)
                                {
                                    itemFound = true;
    
                                    itemNumber = xnStoreItem.InnerText;
                                    itemName = xnStoreItem.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                    itemSize = xnStoreItem.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
                                    unitPrice = double.Parse(xnStoreItem.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText);
                                    strDiscountRate = xnStoreItem.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
    
                                    if ((string.IsNullOrEmpty(strDiscountRate)) || (double.Parse(strDiscountRate) <= 0.00))
                                    {
                                        discountRate = 0.00;
                                        discountAmount = 0.00;
                                        salePrice = unitPrice;
                                    }
                                    else
                                    {
                                        discountRate = double.Parse(strDiscountRate) / 100.00;
                                        discountAmount = unitPrice * discountRate;
                                        salePrice = unitPrice - discountAmount;
                                    }
    
                                    ListViewItem lviStoreItem = new ListViewItem(itemNumber); // Item Number
                                    lviStoreItem.SubItems.Add(itemName); // Item Name
                                    lviStoreItem.SubItems.Add(itemSize); // Item Size
                                    lviStoreItem.SubItems.Add(unitPrice.ToString("F"));
    
                                    if (discountRate == 0.00)
                                    {
                                        lviStoreItem.SubItems.Add(""); // Discount Rate
                                        lviStoreItem.SubItems.Add(""); // Discount Amount
                                        lviStoreItem.SubItems.Add(unitPrice.ToString("F")); // Unit Price
                                    }
                                    else
                                    {
                                        lviStoreItem.SubItems.Add((discountRate * 100).ToString() + "%");
                                        lviStoreItem.SubItems.Add(discountAmount.ToString("F"));
                                        lviStoreItem.SubItems.Add(salePrice.ToString("F"));
                                    }
    
                                    lvwSoldItems.Items.Add(lviStoreItem);
                                }
                            }
    
                            txtItemNumberSelected.Text = "";
    
                            if (itemFound == false)
                            {
                                MessageBox.Show("There is no item with that item number.",
                                                "FunDS - Fun Department Store",
                                                MessageBoxButtons.OK,
                                                MessageBoxIcon.Information);
                            }
                        }
    
                        if (lvwSoldItems.Items.Count > 0)
                        {
                            foreach (ListViewItem lviStoreItem in lvwSoldItems.Items)
                                dblTotal += double.Parse(lviStoreItem.SubItems[6].Text);
    
                            txtOrderTotal.Text = dblTotal.ToString("F");
                        }
                    }
                }
            }
        }
    
        private void ShoppingSessionLoad(object sender, EventArgs e)
        {
        }
    
        private void txtEmployeeNumberLeave(object sender, EventArgs e)
        {
            bool employeeFound = false;
            XmlDocument xdEmployees = new XmlDocument();
            string strEmployeesFile = @"C:\Fun Department Store\Employees.xml";
    
            if (string.IsNullOrEmpty(txtEmployeeNumber.Text))
            {
                return;
            }
            else
            {
                if (File.Exists(strEmployeesFile))
                {
                    using (FileStream fsEmployees = new FileStream(strEmployeesFile, FileMode.Open, FileAccess.Read))
                    {
                        xdEmployees.Load(fsEmployees);
    
                        XmlNodeList xnlEmployees = xdEmployees.GetElementsByTagName("EmployeeNumber");
    
                        foreach (XmlNode xnEmployee in xnlEmployees)
                        {
                            if (txtEmployeeNumber.Text == xnEmployee.InnerText)
                            {
                                employeeFound = true;
                                txtEmployeeName.Text = string.Concat(xnEmployee.NextSibling.NextSibling.InnerText, ", ", xnEmployee.NextSibling.InnerText);
                            }
                        }
    
                        if (employeeFound == false)
                        {
                            MessageBox.Show("There is no employee with that number.",
                                    "FunDS - Fun Department Store",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                        }
                    }
                }
            }
        }
    
        private void txtAmountTenderedKeyUp(object sender, KeyEventArgs e)
        {
            double orderTotal = 0.00;
            double amountTendered = 0.00;
            double change = 0.00;
    
            if ((e.KeyCode == Keys.Tab) || (e.KeyCode == Keys.Enter))
            {
                if (string.IsNullOrEmpty(txtOrderTotal.Text))
                {
                    return;
                }
                else if (string.IsNullOrEmpty(txtAmountTendered.Text))
                {
                    return;
                }
                else
                {
                    orderTotal = double.Parse(txtOrderTotal.Text);
                    amountTendered = double.Parse(txtAmountTendered.Text);
                    change = amountTendered - orderTotal;
    
                    txtChange.Text = change.ToString("F");
                }
            }
        }
    
        private void txtAmountTenderedLeave(object sender, EventArgs e)
        {
            double orderTotal = 0.00;
            double amountTendered = 0.00;
            double change = 0.00;
    
            if (string.IsNullOrEmpty(txtOrderTotal.Text))
            {
                return;
            }
            else if (string.IsNullOrEmpty(txtAmountTendered.Text))
            {
                return;
            }
            else
            {
                orderTotal = double.Parse(txtOrderTotal.Text);
                amountTendered = double.Parse(txtAmountTendered.Text);
                change = amountTendered - orderTotal;
    
                txtChange.Text = change.ToString("F");
            }
        }
    
        private void btnRemoveItemClick(object sender, EventArgs e)
        {
            double dblTotal = 0.00;
            bool itemFound = false;
    
            if (lvwSoldItems.Items.Count == 0)
            {
                MessageBox.Show("The list view is empty.",
                                "FunDS", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else if (string.IsNullOrEmpty(txtItemNumberRemove.Text))
            {
                MessageBox.Show("You must enter an item number and that exists in the list view.",
                                "FunDS", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                foreach (ListViewItem lviStoreItem in lvwSoldItems.Items)
                {
                    if (lviStoreItem.SubItems[0].Text.Equals(txtItemNumberRemove.Text))
                    {
                        itemFound = true;
                        lvwSoldItems.Items.Remove(lviStoreItem);
                    }
                }
    
                if (itemFound == false)
                {
                    MessageBox.Show("That item number is not in the list view.",
                                    "FunDS", MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                    return;
                }
    
                if (lvwSoldItems.Items.Count > 0)
                {
                    foreach (ListViewItem lviStoreItem in lvwSoldItems.Items)
                        dblTotal += double.Parse(lviStoreItem.SubItems[6].Text);
    
                    txtOrderTotal.Text = dblTotal.ToString("F");
                    txtAmountTendered.Text = "0.00";
                    txtChange.Text = "0.00";
                }
                else
                {
                    txtOrderTotal.Text = "0.00";
                    txtAmountTendered.Text = "0.00";
                    txtChange.Text = "0.00";
                }
    
                txtItemNumberRemove.Text = "";
            }
        }
    
        private void btnSubmitClick(object sender, EventArgs e)
        {
            int iSoldItemID = 0;
            XmlDocument xdSoldItems = new XmlDocument();
            XmlDocument xdStoreSales = new XmlDocument();
            XmlDocument xdStoreItems = new XmlDocument();
            string strSoldItemsFile = @"C:\Fun Department Store\SoldItems.xml";
            string strStoreSalesFile = @"C:\Fun Department Store\StoreSales.xml";
            string strStoreItemsFile = @"C:\Fun Department Store\StoreItems.xml";
    
            if (lvwSoldItems.Items.Count == 0)
            {
                MessageBox.Show("There is no customer order to save.",
                                "FunDS", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
    
            if (!File.Exists(strStoreSalesFile))
            {
                using (FileStream fsStoreSales = new FileStream(strStoreSalesFile, FileMode.Create, FileAccess.Write))
                {
                    xdStoreSales.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                                         "<StoreSales></StoreSales>");
                    xdStoreSales.Save(fsStoreSales);
                }
            }
    
            using (FileStream fsStoreSales = new FileStream(strStoreSalesFile, FileMode.Open, FileAccess.Read))
            {
                xdStoreSales.Load(fsStoreSales);
            }
    
            using (FileStream fsStoreSales = new FileStream(strStoreSalesFile, FileMode.Create, FileAccess.Write))
            {
                XmlElement xeStoreSale = xdStoreSales.CreateElement("StoreSale");
                xeStoreSale.InnerXml = "<ReceiptNumber>" + txtReceiptNumber.Text + "</ReceiptNumber>" +
                                       "<EmployeeNumber>" + txtEmployeeNumber.Text + "</EmployeeNumber>" +
                                       "<SaleDate>" + dtpSaleDate.Value.ToShortDateString() + "</SaleDate>" +
                                       "<SaleTime>" + lblSaleTime.Text + "</SaleTime>" +
                                       "<OrderTotal>" + txtOrderTotal.Text + "</OrderTotal>" +
                                       "<AmountTendered>" + txtAmountTendered.Text + "</AmountTendered>" +
                                       "<Change>" + txtChange.Text + "</Change>";
                xdStoreSales.DocumentElement.AppendChild(xeStoreSale);
    
                xdStoreSales.Save(fsStoreSales);
            }
    
            if (!File.Exists(strSoldItemsFile))
            {
                using (FileStream fsSoldItems = new FileStream(strSoldItemsFile, FileMode.Create, FileAccess.Write))
                {
                    xdSoldItems.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                                        "<SoldItems></SoldItems>");
                    xdSoldItems.Save(fsSoldItems);
                }
            }
    
            using (FileStream fsSoldItems = new FileStream(strSoldItemsFile, FileMode.Open, FileAccess.Read))
            {
                xdSoldItems.Load(fsSoldItems);
                XmlNodeList xnlSoldItems = xdSoldItems.GetElementsByTagName("SoldItemID");
    
                foreach (XmlNode xnSoldItem in xnlSoldItems)
                    iSoldItemID = int.Parse(xnSoldItem.InnerText);
            }
    
            iSoldItemID = iSoldItemID + 1;
    
            using (FileStream fsSoldItems = new FileStream(strSoldItemsFile, FileMode.Create, FileAccess.Write))
            {
                foreach (ListViewItem lviSoldItem in lvwSoldItems.Items)
                {
                    XmlElement xeSoldItem = xdSoldItems.CreateElement("SoldItem");
                    xeSoldItem.InnerXml = "<SoldItemID>" + iSoldItemID.ToString() + "</SoldItemID>" +
                                          "<ReceiptNumber>" + txtReceiptNumber.Text + "</ReceiptNumber>" +
                                          "<ItemNumber>" + lviSoldItem.SubItems[0].Text + "</ItemNumber>" +
                                          "<ItemName>" + lviSoldItem.SubItems[1].Text + "</ItemName>" +
                                          "<ItemSize>" + lviSoldItem.SubItems[2].Text + "</ItemSize>" +
                                          "<UnitPrice>" + lviSoldItem.SubItems[3].Text + "</UnitPrice>" +
                                          "<DiscountRate>" + lviSoldItem.SubItems[4].Text + "</DiscountRate>" +
                                          "<DiscountAmount>" + lviSoldItem.SubItems[5].Text + "</DiscountAmount>" +
                                          "<SalePrice>" + lviSoldItem.SubItems[6].Text + "</SalePrice>";
                    xdSoldItems.DocumentElement.AppendChild(xeSoldItem);
                    iSoldItemID++;
                }
    
                xdSoldItems.Save(fsSoldItems);
    
                MessageBox.Show("The customer's order has been saved.",
                                "FunDS", MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
            }
    
            if (File.Exists(strStoreItemsFile))
            {
                using (FileStream fsStoreItems = new FileStream(strStoreItemsFile, FileMode.Open, FileAccess.Read))
                {
                    xdStoreItems.Load(fsStoreItems);
    
                    XmlNodeList xnlStoreItems = xdStoreItems.GetElementsByTagName("ItemNumber");
    
                    // Remove the items of the list view from the StoreItems table
                    foreach (ListViewItem lviStoreItem in lvwSoldItems.Items)
                    {
                        foreach (XmlNode xnStoreItem in xnlStoreItems)
                        {
                            if (xnStoreItem.InnerText == lviStoreItem.SubItems[0].Text)
                                xdStoreItems.DocumentElement.RemoveChild(xnStoreItem.ParentNode);
                        }
                    }
                }
    
                using (FileStream fsStoreItems = new FileStream(strStoreItemsFile, FileMode.Create, FileAccess.Write))
                {
                    xdStoreItems.Save(fsStoreItems);
                }
            }
    
            ResetForm();
        }
    
        private void tmrDateTimeTick(object sender, EventArgs e)
        {
            dtpSaleDate.Value = DateTime.Today;
            lblSaleTime.Text = DateTime.Now.ToLongTimeString();
        }
    
        private void btnCloseClick(object sender, EventArgs e)
        {
            Close();
        }
    }
  2. On the main menu, click File -> New
  3. When asked whether you want to save, click Save
  4. In the top combo box, make sure the FunDS1 folder is displaying, Set the Save As Type to All Files
  5. Set the File Name to ShoppingSessionReview.cs
  6. Click Save

A Switchboard

A switchboard serves as the central point of an application. It holds a list of the forms used in an application.

Practical LearningPractical Learning: Creating a Switchboard

  1. In the empty document, type the following:
    using System;
    using System.IO;
    using System.Xml;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Collections.Generic;
    
    public class FunDS : Form
    {
        private Button btnNewShoppingSession;
        private Button btnStoreItems;
        private Button btnShoppingSessionReview;
        private Button btnNewStoreItem;
        private Button btnEmployees;
        private Button btnEditStoreItem;
        private Button btnNewEmployee;
        private Button btnDeleteStoteItem;
        private Button btnClose;
    
        public FunDS()
        {
            InitializeComponent();
        }
    
        private void InitializeComponent()
        {
            // Button: New Shopping Session
            btnNewShoppingSession = new Button();
            btnNewShoppingSession.Font = new System.Drawing.Font("Palatino Linotype", 26.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            btnNewShoppingSession.Location = new Point(22, 22);
            btnNewShoppingSession.Size = new System.Drawing.Size(478, 67);
            btnNewShoppingSession.TabIndex = 0;
            btnNewShoppingSession.Text = "New Shopping Session ...";
            btnNewShoppingSession.Click += new System.EventHandler(btnNewShoppingSessionClick);
            Controls.Add(btnNewShoppingSession);
    
            // Button: Store Items
            btnStoreItems = new Button();
            btnStoreItems.Font = new System.Drawing.Font("Palatino Linotype", 26.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            btnStoreItems.Location = new Point(518, 22);
            btnStoreItems.Size = new System.Drawing.Size(478, 67);
            btnStoreItems.TabIndex = 1;
            btnStoreItems.Text = "Store Items ...";
            btnStoreItems.Click += new System.EventHandler(btnStoreItemsClick);
            Controls.Add(btnStoreItems);
    
            // Button: Shopping Session  Review
            btnShoppingSessionReview = new Button();
            btnShoppingSessionReview.Font = new System.Drawing.Font("Palatino Linotype", 26.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            btnShoppingSessionReview.Location = new Point(22, 108);
            btnShoppingSessionReview.Size = new System.Drawing.Size(478, 67);
            btnShoppingSessionReview.TabIndex = 2;
            btnShoppingSessionReview.Text = "Shopping Session Review ...";
            btnShoppingSessionReview.Click += new System.EventHandler(btnShoppingSessionReviewClick);
            Controls.Add(btnShoppingSessionReview);
    
            // Button: New Store Item
            btnNewStoreItem = new Button();
            btnNewStoreItem.Font = new System.Drawing.Font("Palatino Linotype", 26.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            btnNewStoreItem.Location = new Point(518, 108);
            btnNewStoreItem.Size = new System.Drawing.Size(478, 67);
            btnNewStoreItem.TabIndex = 3;
            btnNewStoreItem.Text = "New Store Item ...";
            btnNewStoreItem.Click += new System.EventHandler(btnNewStoreItemClick);
            Controls.Add(btnNewStoreItem);
    
            // Button: Employees
            btnEmployees = new Button();
            btnEmployees.Font = new System.Drawing.Font("Palatino Linotype", 26.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            btnEmployees.Location = new Point(22, 194);
            btnEmployees.Size = new System.Drawing.Size(478, 67);
            btnEmployees.TabIndex = 4;
            btnEmployees.Text = "Employees ...";
            btnEmployees.Click += new System.EventHandler(btnEmployeesClick);
            Controls.Add(btnEmployees);
    
            // Button: Edit Store Item
            btnEditStoreItem = new Button();
            btnEditStoreItem.Font = new System.Drawing.Font("Palatino Linotype", 26.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            btnEditStoreItem.Location = new Point(518, 194);
            btnEditStoreItem.Size = new System.Drawing.Size(478, 67);
            btnEditStoreItem.TabIndex = 5;
            btnEditStoreItem.Text = "Edit Store Item ...";
            btnEditStoreItem.Click += new System.EventHandler(btnEditStoreItemClick);
            Controls.Add(btnEditStoreItem);
    
            // Button: New Employee
            btnNewEmployee = new Button();
            btnNewEmployee.Font = new System.Drawing.Font("Palatino Linotype", 26.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            btnNewEmployee.Location = new Point(22, 283);
            btnNewEmployee.Size = new System.Drawing.Size(478, 67);
            btnNewEmployee.TabIndex = 6;
            btnNewEmployee.Text = "New Employee ...";
            btnNewEmployee.Click += new System.EventHandler(btnNewEmployeeClick);
            Controls.Add(btnNewEmployee);
    
            // Button: Delete Stote Item
            btnDeleteStoteItem = new Button();
            btnDeleteStoteItem.Font = new System.Drawing.Font("Palatino Linotype", 26.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            btnDeleteStoteItem.Location = new Point(518, 283);
            btnDeleteStoteItem.Size = new System.Drawing.Size(478, 67);
            btnDeleteStoteItem.TabIndex = 7;
            btnDeleteStoteItem.Text = "Delete Stote Item ...";
            btnDeleteStoteItem.Click += new System.EventHandler(btnDeleteStoteItemClick);
            Controls.Add(btnDeleteStoteItem);
    
            // Button: Close
            btnClose = new Button();
            btnClose.Font = new System.Drawing.Font("Palatino Linotype", 27.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
            btnClose.Location = new Point(22, 374);
            btnClose.Size = new System.Drawing.Size(974, 67);
            btnClose.TabIndex = 8;
            btnClose.Text = "Close";
            btnClose.Click += new System.EventHandler(btnCloseClick);
            Controls.Add(btnClose);
    
            // Form: Fun Department Store
            ClientSize = new System.Drawing.Size(1018, 463);
            StartPosition = FormStartPosition.CenterScreen;
            Text = "FunDS - Fun Department Store";
            Load += new EventHandler(FunDSLoad);
        }
    
        private void FunDSLoad(object sender, EventArgs e)
        {
            Directory.CreateDirectory(@"C:\Fun Department Store");
        }
    
        private void btnNewShoppingSessionClick(object sender, EventArgs e)
        {
            NewShoppingSession nss = new NewShoppingSession();
            nss.Show();
        }
    
        private void btnStoreItemsClick(object sender, EventArgs e)
        {
            StoreItems sis = new StoreItems();
            sis.Show();
        }
    
        private void btnShoppingSessionReviewClick(object sender, EventArgs e)
        {
            ShoppingSessionReview ssr = new ShoppingSessionReview();
            ssr.Show();
        }
    
        private void btnNewStoreItemClick(object sender, EventArgs e)
        {
            StoreItemNew sin = new StoreItemNew();
            sin.ShowDialog();
        }
    
        private void btnEmployeesClick(object sender, EventArgs e)
        {
            Employees clerks = new Employees();
            clerks.Show();
        }
    
        private void btnEditStoreItemClick(object sender, EventArgs e)
        {
            StoreItemEditor sie = new StoreItemEditor();
            sie.ShowDialog();
        }
    
        private void btnNewEmployeeClick(object sender, EventArgs e)
        {
            string strEmployeeNumber, strFirstName, strLastName, strCounty,
                   strAddress, strCity, strState, strZIPCode, strMaritalStatus;
            int iExemptions;
            double dblHourlySalary;
    
            EmployeeNew en = new EmployeeNew();
            Random rndEmployeeNumber = new Random();
            XmlDocument xdEmployees = new XmlDocument();
            string strEmployeesFile = @"C:\Fun Department Store\Employees.xml";
    
            en.txtEmployeeNumber.Text = rndEmployeeNumber.Next(100000, 999999).ToString();
    
            if (en.ShowDialog() == DialogResult.OK)
            {
                strEmployeeNumber = en.txtEmployeeNumber.Text;
                strFirstName = en.txtFirstName.Text;
                strLastName = en.txtLastName.Text;
                strMaritalStatus = en.cbxMaritalsStatus.Text;
                iExemptions = int.Parse(en.txtExemptions.Text);
                strAddress = en.txtAddress.Text;
                strCity = en.txtCity.Text;
                strCounty = en.txtCounty.Text;
                strState = en.txtState.Text;
                strZIPCode = en.txtZIPCode.Text;
                dblHourlySalary = double.Parse(en.txtHourlySalary.Text);
    
                if (!File.Exists(strEmployeesFile))
                {
                    xdEmployees.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                                        "<Employees></Employees>");
                    xdEmployees.Save(strEmployeesFile);
                }
    
                xdEmployees.Load(strEmployeesFile);
    
                XmlElement elmXML = xdEmployees.CreateElement("Employee");
                // Create the XML code of the child element of Employee
                string strNewEmployee = "<EmployeeNumber>" + strEmployeeNumber + "</EmployeeNumber>" +
                                        "<FirstName>" + strFirstName + "</FirstName>" +
                                        "<LastName>" + strLastName + "</LastName>" +
                                        "<Address>" + strAddress + "</Address>" +
                                        "<City>" + strCity + "</City>" +
                                        "<County>" + strCounty + "</County>" +
                                        "<State>" + strState + "</State>" +
                                        "<ZIPCode>" + strZIPCode + "</ZIPCode>" +
                                        "<MaritalStatus>" + strMaritalStatus + "</MaritalStatus>" +
                                        "<Exemptions>" + iExemptions + "</Exemptions>" +
                                        "<HourlySalary>" + dblHourlySalary + "</HourlySalary>";
                elmXML.InnerXml = strNewEmployee;
                // Append the new element as a child of employees
                xdEmployees.DocumentElement.AppendChild(elmXML);
    
                // Save the XML file
                xdEmployees.Save(strEmployeesFile);
            }
        }
    
        private void btnDeleteStoteItemClick(object sender, EventArgs e)
        {
            StoreItemDelete sid = new StoreItemDelete();
            sid.ShowDialog();
        }
    
        private void btnCloseClick(object sender, EventArgs e)
        {
            Close();
        }
    
        [STAThread]
        public static int Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new FunDS());
    
            return 0;
        }
    }
  2. On the main menu, click File -> Exit
  3. When asked whether you want to save, click Save
  4. In the top combo box, make sure the FunDS1 folder is displaying, Set the Save As Type to All Files
  5. Set the File Name to FunDS.cs
  6. Click Save

Building the Application

To build this project, you can use the compiler provides in Microsoft Windows and from the Command Prompt.

Practical LearningPractical Learning: Creating a Switchboard

  1. Display the Command Prompt (Start -> (All) Programs -> Accessories -> Command Prompt)
  2. Type CD\ and press Enter to access the root
  3. Type CD FunDS1 and press Enter to access the folder for the current project
  4. To build the project, type the following code:
    C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc /t:winexe FunDS.cs EmployeeNew.cs EmployeeEditor.cs Employees.cs Manufacturer.cs Category.cs SubCategory.cs StoreItemNew.cs StoreItemEditor.cs StoreItemDelete.cs Inventory.cs PointOfSale.cs ShoppingSessionReview.cs
  5. Press Enter

Application Testing

If you want to test the application, use the following values.

 
 
   
                  

Practical LearningPractical Learning: Testing the Application

  1. At the Command Prompt, type FunDS and press Enter
  2. On the FunDS switchboard, click the New Employee button to open its dialog box
  3. Fill the controls with the following values:
    Employee #: 941148
    First Name: Catherine
    Last Name: Watts
    Address: 4242 Sightings Str
    City: Baltimore
    Country: Baltimore
    State: MD
    ZIP Code: 21205
    Marital Status: Married
    Exemptions: 3
    Hourly Salary: 38.85
  4. Click OK
  5. On the switchboard, click the New Store Item... button
  6. Create items as follows (create the manufacturers, the categories, and the sub-categories by clicking the corresponding buttons):
     
    Item # Manufacturer Category Sub-Category Item Name Item Size Unit Price Discount Rate
    177314 Ralph Lauren Girls Shirts Girls 2-6X Short-Sleeved Mesh Polo Shirt 3T 34.95  
    729530 Kenneth Cole Women Dresses Three-Quarter Sleeved Dress M 164.5 50
    307368 Polo Ralph Lauren Men Pants Classic Straight-Leg Jeans 30W - 29L 84.85 15
    220573 Nautica Baby Girls   Baby Girls 12-24 Months Lace Two-Piece Set 12M 40  
    806888 Guess Girls Dresses Girls 2-6X Denim Print Ruffled Dress 2T 34.95  
    683079 Anne Klein Women Skirts Pencil Skirt 2 54  
    848428 Ralph Lauren Boys Sweaters Boys 2-7 Long-Sleeved Cable Crewneck T-Shirt 4 55  
    683626 Ralph Lauren Women Dresses Structured Scoopneck Ponte Dress 0 195 60
  7. Close the New Store Item dialog box
  8. On the switchboard, click the New Shopping Session button
  9. Set the employee number as 941148 and press Tab
  10. In the Item # To Add text box, type 848428 and press Enter
  11. Type 729530 and press Enter
  12. In the Tendered text box, enter 200 and press Enter:

    Fun Department Store - New Shopping Session

  13. Click Submit
  14. Start a new shopping session with employee number as 941148 and press Tab
  15. Add the shopping items with the following numbers: 683626, 307368, 683079, and 806888
  16. In the Item # to Remove text box, type 683079

    Fun Department Store - New Shopping Session

  17. Click Remove
  18. In the Tendered text box, type 200 and press Enter

    Fun Department Store - New Shopping Session

  19. Click Submit
  20. Close the New Shopping Session form
  21. On the Switchboard, click the Shopping Session Review button
  22. In the Receipt Number text box, type 100001 and click Find

    Fun Department Store - Shopping Session Review

  23. Closse the Shopping Session Review form
  24. On the switchboard, click the Employees button
  25. Use the New Employee button to create a few records as follows:
     
    Employee # First Name Last Name Address City Country State ZIP Code Marital Status Exemptions Hourly Salary
    952748 David Evans 5102 Piedmont Rd Silver Spring Montgomery MD 20910 Single 0 17.25
    606384 Robert Gibson 10324 Marina Ave College Park Prince George MD 20742 Single 1 22.25
  26. Close the Employees form
  27. Click the New Shopping Session button and create a new shopping session as follows:
     
    Employee # Item # Tendered
    606384 177314 50
  28. Click Submit
  29. Close the New Shopping Session form
  30. Click the Employees button and create a few emplyees records as follows:
     
    Employee # First Name Last Name Address City Country State ZIP Code Marital Status Exemptions Hourly Salary
    428041 Jeannine Hewsen  2418 Woodwell Rd Rockville Montgomery MD 20850 Married 2 34.05
    172847 Carl Lowry 7447 Emiry Str Alexandria (Independent) VA 22314 Single 2 26.85
    927048 Henry Meuer 802 Wheeler St York York PA 17401 Married 0 8.95
    837405 Herbert Mann  6218 Willimon Ave Wilmington New Castle DE 19801 Married 2 14.75
  31. Close the Employees form
  32. On the switchboard, click the Store Items button
  33. Click New Store Item...
  34. Create items as follows:
     
    Item # Manufacturer Category Sub-Category Item Name Item Size Unit Price Discount Rate
    580040 Cole Haan Women Handbags Victoria Zip-Top Tote Bag   298  
    795081 Lauren by Ralph Lauren Men Pants Mid-Weight Flat-Front Wool Trouser Pants 36W 32L 65 25
    948596 Tommy Hilfiger Men Shirts Texture Oxford Slim Fit Long Sleeve Dress Shirt 17 - 34/35 47.75  
    360825 Lauren by Ralph Lauren Women Accessories Printed Silk Scarf   45  
    383511 Calvin Klein Men Pants Straight Leg Jean in Black Wash 32/30 45  
    294148 Lauren by Ralph Lauren Men Pants Mid-Weight Flat-Front Wool Trouser Pants 36W 29L 65  
    559717 Guess Girls Dresses Girls 2-6X Denim Print Ruffled Dress 3T 34.95  
    830486 AK Anne Klein Women Skirts Seamless Textured Pencil Skirt S 65.95  
  35. Close the New Store Item dialog box
  36. Close the Store Items form
  37. Click the New Shopping Session button and create a new shopping session as follows:
     
    Employee # Item # Tendered
    428041 683079  
      360825  
      830486  
      383511  
      948596 260
  38. Close the New Shopping Session form
  39. On the Switchboard, click the Shopping Session Review button
  40. In the Receipt Number text box, type 100003 and click Find

    Fun Department Store - Shopping Session Review

  41. Close the form and close Microsoft Access

Previous Copyright © 2015 FunctionX Next