Exemple #1
0
        // Initial method to load product groups and products
        /* ----- METHOD ----- */
        private void LoadProducts()
        {
            // Setting applications Culture info to always use comma as decimal separator
            CultureInfo CI = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();

            CI.NumberFormat.NumberDecimalSeparator = ",";
            Thread.CurrentThread.CurrentCulture    = CI;

            using (DataSet ds = new DataSet())
            {
                // Read in Productgroups from XML file
                Stream stream = File.OpenRead(Environment.CurrentDirectory + @"\Resources\ProductGroups.xml");
                ds.ReadXml(stream, XmlReadMode.InferSchema);
                stream.Close();
                DataTable dt = ds.Tables[0];

                // assign global variable
                ProductGroups = new ProductGroup[dt.Rows.Count];

                // Loop through read data and populate array with new ProductGroup objects
                int i = 0;
                foreach (DataRow row in dt.Rows)
                {
                    ProductGroups[i] = new ProductGroup(Int32.Parse(row.ItemArray[0].ToString()), row.ItemArray[1].ToString());
                    i++;
                }
            }

            using (DataSet ds = new DataSet())
            {
                // Read in Products from XML File
                Stream stream = File.OpenRead(Environment.CurrentDirectory + @"\Resources\Products.xml");
                ds.ReadXml(stream, XmlReadMode.InferSchema);
                stream.Close();
                DataTable dt = ds.Tables[0];

                // assign global variable
                Products = new Product[dt.Rows.Count];

                // Loop through read data and populate array with Product objects
                int i = 0;
                foreach (DataRow row in dt.Rows)
                {
                    Products[i] = new Product(Int32.Parse(row.ItemArray[0].ToString()), Int32.Parse(row.ItemArray[1].ToString()), row.ItemArray[2].ToString(), Double.Parse(row.ItemArray[3].ToString()));
                    i++;
                }
            }

            int x = selectablePanel.DisplayRectangle.Left + 5;
            int y = selectablePanel.DisplayRectangle.Top + 10;

            // Create UI Elements for Selectable Products/Groups

            // Loop through product groups
            int j = 0;

            foreach (ProductGroup group in ProductGroups)
            {
                if (j > 0)
                {
                    y += 25;
                }
                // For each group create label
                Label groupLabel = new Label
                {
                    Name     = group.GetName() + "Label",
                    Text     = group.GetName(),
                    Location = new Point(x, y),
                    Height   = 20
                };

                // Array to store Product objects pertaining to each group
                Product[] groupProds = Products.Where(o => o.GetGroupIndex() == group.GetIndex()).ToArray <Product>();

                y += 20;

                // Create Dropdown for each group
                ComboBox groupDdwn = new ComboBox
                {
                    Name          = group.GetName() + "Ddwn",
                    Location      = new Point(x, y),
                    DropDownStyle = ComboBoxStyle.DropDownList
                };

                // Loop through each Product in each group and add to dropdown
                foreach (Product product in groupProds)
                {
                    groupDdwn.Items.Add(product.GetName() + " " + product.GetPrice());
                }

                // Create one text box for each group to keep count of number of products user wishes to add
                TextBox tbCount = new TextBox
                {
                    Name     = group.GetName() + "Count",
                    Width    = 25,
                    Height   = 20,
                    Location = new Point(groupDdwn.DisplayRectangle.Right + 25, y),
                };

                // Add controls to panel
                selectablePanel.Controls.Add(groupLabel);
                selectablePanel.Controls.Add(groupDdwn);
                selectablePanel.Controls.Add(tbCount);

                // Add eventhandlers to controls
                tbCount.KeyPress += new KeyPressEventHandler(DisallowText);
                groupDdwn.SelectedIndexChanged += new EventHandler(OnDdwnSelect);
                tbCount.Invalidated            += new InvalidateEventHandler(OnCountUpdate);
                tbCount.Leave += new EventHandler(OnCountUpdate);

                y += 5;
                j++;
            }

            // Header label for count textbox
            Label countLabel = new Label
            {
                Text     = "Count",
                Location = new Point(140, selectablePanel.DisplayRectangle.Top + 10)
            };

            selectablePanel.Controls.Add(countLabel);
        }
Exemple #2
0
        // Event Handler method for adding products
        /* ----- METHOD ----- */
        private void AddProduct(object sender, EventArgs e)
        {
            // int to control index in 2D array
            int x = 0;

            // Loop through 2D array to find the next empty index
            for (int i = 0; i < GroceryList.GetLength(0); i++)
            {
                if (GroceryList[i, 0] != null)
                {
                    x++;
                }
                else
                {
                    break;
                }
            }

            // int to represent count of products entered by user
            int count = 0;

            // Empty Product object to be used for adding
            Product addedProd = null;

            // Loop through the controls of left hand panel
            foreach (Control control in selectablePanel.Controls)
            {
                // if control is text box store entered number in count variable and leave 0 if nothing is entered
                // reset the text to empty
                if (control.GetType() == typeof(TextBox))
                {
                    TextBox countTb = (TextBox)control;
                    Int32.TryParse(countTb.Text, out count);
                    countTb.Text = "";
                }

                // if control is dropdown
                if (control.GetType() == typeof(ComboBox))
                {
                    ComboBox prodCb = (ComboBox)control;

                    // ProductGroup object corresponding to selected dropdown group
                    ProductGroup addedProdGroup = ProductGroups.FirstOrDefault(pg => pg.GetName() == prodCb.Name.Substring(0, prodCb.Name.Length - 4));

                    // Set empty Product object to selected object based on group and selected item in dropdown
                    addedProd = Products.FirstOrDefault(p => p.GetGroupIndex() == addedProdGroup.GetIndex() && p.GetIndex() == prodCb.SelectedIndex);

                    // reset dropdown select to empty
                    prodCb.SelectedIndex = -1;
                }

                // If the count entered is larger than 0
                if (count > 0)
                {
                    // set the corresponding positions in 2D array to correspond to selected product and count thereof
                    GroceryList[x, 0] = addedProd;
                    GroceryList[x, 1] = count;

                    // Reset Count variable
                    count = 0;

                    // Update index variable
                    x++;
                }
            }

            // Call method to present added products
            LoadList(GroceryList);
        }