private void AddToCartButtonClickedInUserControl(object sender, EventArgs e) { // Cast object class to MyUserControl MyUserControl myControl = sender as MyUserControl; int productId = myControl.DrinkID; AddItemToCart(productId); }
private void OnButtonAddToCartClicked(object sender, EventArgs e) { MyUserControl myUserControl = sender as MyUserControl; IBeverage drink = beverageRepo.GetDrink(myUserControl.DrinkID); for (int i = 0; i < myUserControl.Amount; i++) { shoppingCart.AddDrinkToCart(drink); } }
private void GenerateControlsForDrinks(IEnumerable <IBeverage> drinks) { foreach (IBeverage drink in drinks) { MyUserControl myUserControl = new MyUserControl { DrinkID = drink.ID, MyProductName = drink.Name, Price = drink.Price.ToString(), //IsInStock = drink.IsInStock, Description = drink.Description, Image = drink.Image, }; myUserControl.ButtonAddToCartClicked += OnButtonAddToCartClicked; flowLayoutPanel1.Controls.Add(myUserControl); } }
//evt: inkorten code private void GenerateControlsForBeverages(IEnumerable <IBeverage> beverages) { foreach (IBeverage beverage in beverages) { if (beverage is Beer) { Beer beer = beverage as Beer; UserControlBeer UserControlBeer = new UserControlBeer() { BeverageID = beverage.ID, BeverageName = beverage.Name, Description = beverage.Discription, Price = beverage.Price.ToString(), Image = beverage.Image, AlcoholPercentage = beer.AlcoholPercentage, IsTrapist = beer.IsTrapist, Type = beer.Type }; //listener wordt uitgevoerd wanneer dat een buttonorderclicked in usercontrolbeer wordt aangevinkt UserControlBeer.ButtonOrderClicked += ButtonAddToCartInUserControlWasClicked; flowLayoutPanel1.Controls.Add(UserControlBeer); } else if (beverage is Soda) { Soda soda = beverage as Soda; UserControlSoda UserControlSoda = new UserControlSoda() { BeverageID = beverage.ID, BeverageName = beverage.Name, Description = beverage.Discription, Price = beverage.Price.ToString(), Image = beverage.Image, Extras = soda.Extras, WithSugar = soda.WithSugar }; UserControlSoda.ButtonOrderClicked += ButtonAddToCartInUserControlWasClicked; flowLayoutPanel1.Controls.Add(UserControlSoda); } else if (beverage is Smoothie) { Smoothie smoothie = beverage as Smoothie; UserControlSmoothie UserControlSmoothie = new UserControlSmoothie() { BeverageID = beverage.ID, BeverageName = beverage.Name, Description = beverage.Discription, Price = beverage.Price.ToString(), Image = beverage.Image, ExtraFruits = smoothie.ExtraFruits, BeverageSize = smoothie.Size }; UserControlSmoothie.ButtonOrderClicked += ButtonAddToCartInUserControlWasClicked; flowLayoutPanel1.Controls.Add(UserControlSmoothie); } else { MyUserControl myUserControl = new MyUserControl() { BeverageID = beverage.ID, BeverageName = beverage.Name, Description = beverage.Discription, Price = beverage.Price.ToString(), Image = beverage.Image, }; myUserControl.ButtonOrderClicked += ButtonAddToCartInUserControlWasClicked; //op einde allemaal mooi aan panel toevoegen flowLayoutPanel1.Controls.Add(myUserControl); } } }