private void PlusBtn_Click(object sender, EventArgs e) { //Finding the required parent controls Button btnSender = (Button)sender; GroupBox gbSender = (GroupBox)btnSender.Parent; BasketGBControl endParent = (BasketGBControl)gbSender.Parent; Product prod = endParent.paramProd; //Looping through the available products o find the right one //Decreasing its quantity, increasing the quantity in the basket //Or showing the error message if no more items can be added to the basket foreach (Product productAv in ShopForm.AvailableProducts) { if (productAv.Equals(prod)) { if (productAv.Quantity > 0) { prod.Quantity++; endParent.TrueQtyLbl.Text = prod.Quantity.ToString(); productAv.Quantity--; ShopGBControl.QtyChange(endParent, productAv); ShopForm.ChangePriceWeightLbls(endParent, Basket.BasketList); return; } else { MessageBox.Show($"Sorry, {prod.Name.ToLower()} is out of stock."); return; } } } }
private void MinusBtn_Click(object sender, EventArgs e) { //Finding the required parent controls Button btnSender = (Button)sender; GroupBox gbSender = (GroupBox)btnSender.Parent; BasketGBControl endParent = (BasketGBControl)gbSender.Parent; FlowLayoutPanel flpParent = (FlowLayoutPanel)endParent.Parent; Product prod = endParent.paramProd; //Checking the quantity of item in the basket and performing necessary actions accordingly //Decreasing quantity in basket, increasing quantity in available products, rerendering the labels //And, if necessary, removing the item from the basket if (prod.Quantity > 1) { prod.Quantity--; endParent.TrueQtyLbl.Text = prod.Quantity.ToString(); foreach (Product productAv in ShopForm.AvailableProducts) { if (productAv.Equals(prod)) { productAv.Quantity++; ShopGBControl.QtyChange(endParent, productAv); ShopForm.ChangePriceWeightLbls(endParent, Basket.BasketList); return; } } } else if (prod.Quantity == 1) { foreach (Product productAv in ShopForm.AvailableProducts) { if (productAv.Equals(prod)) { productAv.Quantity++; ShopGBControl.QtyChange(endParent, productAv); ShopForm.ChangePriceWeightLbls(endParent, Basket.BasketList); Basket.BasketList.Remove(prod); flpParent.Controls.Remove(endParent); return; } } } }
public static void AddToBasket(Object obj, EventArgs e) { //Adds a product to the basket Button addBtn = (Button)obj; GroupBox groupBox = (GroupBox)addBtn.Parent; ShopGBControl endParent = (ShopGBControl)groupBox.Parent; int prodQty = endParent.paramProd.Quantity; string boxName = groupBox.Text; //Loops through available products and checks if the name of ShopGBControl (the sender) //matches with the neame of the product, the quantity is more than 0 and the product //is not yet in the Basket and then adds it to the temporary basket. //Displays the appropriate error messages if a certain criterion is not met. foreach (Product prod in ShopForm.AvailableProducts) { if (prod.Name == boxName && prod.Quantity > 0 && !Basket.BasketList.Contains(prod)) { prodQty--; endParent.TrueQtyLbl.Text = prodQty.ToString(); Basket.TempBasketList.Add(prod); Basket.RenderBasket(groupBox); ShopForm.ChangePriceWeightLbls(endParent, Basket.BasketList); return; } else if (prod.Name == boxName && prod.Quantity == 0) { MessageBox.Show($"Sorry, the {prod.Name.ToLower()} is out of stock."); return; } else if (prod.Name == boxName && Basket.BasketList.Contains(prod)) { MessageBox.Show("The product is already in the basket!"); return; } } MessageBox.Show("An error has occured."); }
public static void RenderBasket(GroupBox gbSender) { //Finding the final parent (the main form (ShopForm)) ShopGBControl sgbcSender = (ShopGBControl)gbSender.Parent; FlowLayoutPanel flpSender = (FlowLayoutPanel)sgbcSender.Parent; Form baseForm = (Form)flpSender.Parent; //Generating an IEnumerable of required type var flpList = baseForm.Controls.OfType <FlowLayoutPanel>(); //Looping through the flpList, looking for the needed FlowLayoutPanel //Checking whether the item is already in there and adding it if needed //Or displaying an error message foreach (var flp in flpList) { if (flp.Name == "BasketPanel") { for (int i = 0; i < TempBasketList.Count; i++) { Product prod = TempBasketList[i]; if (!BasketList.Contains(prod)) { prod.Quantity--; Product basketProd = new Product(prod); BasketList.Add(basketProd); flp.Controls.Add(new BasketGBControl(basketProd)); TempBasketList.Clear(); } else { TempBasketList.Clear(); MessageBox.Show("Product is in the basket already"); } } } } }