private void GetProduct_Click(object sender, RoutedEventArgs e) { Button button = sender as Button; string name = button.Content.ToString().Remove(0, 4); BuyedProduct product = buyedProducts.Where(a => a.Name == name).Select(a => a).FirstOrDefault(); TextBox amountTextBox = (TextBox)button.Tag; if (int.TryParse(amountTextBox.Text, out int result)) { if (result <= product.CurrentStock) { int amount = int.Parse(amountTextBox.Text); product.Amount = amount; currentStock.Content = "Available: " + (product.CurrentStock - product.Amount) + " kg"; product.CurrentStock = product.CurrentStock - product.Amount; SendToBasket(name, amount, amount * product.Price); amountTextBox.Clear(); } else { MessageBox.Show("Tyvärr, vi har inga tillräkliga varor!"); } } else { MessageBox.Show("Du kan bara skriva nummer!"); } }
public void GetProducts() { ReadSavedItems(); products = File.ReadAllLines("Produkter.csv"); foreach (string product in products) { string[] splittedPruduct = product.Split(','); StackPanel productStackPanel = new StackPanel() { Orientation = Orientation.Vertical, Margin = new Thickness(10), Width = 155, Height = 250, Background = Brushes.White, VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center }; string path = ("Bilder/" + splittedPruduct[0]); ImageSource source = new BitmapImage(new Uri(path, UriKind.Relative)); Image image = new Image { Source = source, Width = 100, Height = 100, Stretch = Stretch.Fill, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, Margin = new Thickness(5) }; Label productName = new Label { Content = splittedPruduct[1], FontSize = 18, FontWeight = FontWeights.Bold, Padding = new Thickness(10, 0, 0, 0) }; Label productDescription = new Label { Content = splittedPruduct[2], Padding = new Thickness(10, 0, 0, 0) }; Label productPrice = new Label { Content = "Price: " + splittedPruduct[3] + "/kg", Padding = new Thickness(10, 5, 0, 0), FontWeight = FontWeights.SemiBold, FontSize = 14 }; currentStock = new Label { Content = "Available: " + splittedPruduct[4] + " kg", Padding = new Thickness(10, 5, 0, 0) }; StackPanel amountAndBuy = new StackPanel() { Orientation = Orientation.Horizontal, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, Margin = new Thickness(10) }; Button buyProduct = new Button() { Content = "Buy " + splittedPruduct[1], Width = 100, Height = 20, Margin = new Thickness(5) }; TextBox amount = new TextBox() { Margin = new Thickness(2), TextAlignment = TextAlignment.Center, Width = 25, Height = 20, MaxLength = 3, }; buyProduct.Tag = amount; BuyedProduct buyedProduct = new BuyedProduct() { Name = splittedPruduct[1], Amount = 0, Price = int.Parse(splittedPruduct[3]), CurrentStock = int.Parse(splittedPruduct[4]) }; buyedProducts.Add(buyedProduct); amountAndBuy.Children.Add(amount); amountAndBuy.Children.Add(buyProduct); productStackPanel.Children.Add(image); productStackPanel.Children.Add(productName); productStackPanel.Children.Add(productDescription); productStackPanel.Children.Add(productPrice); productStackPanel.Children.Add(currentStock); productStackPanel.Children.Add(amountAndBuy); productWrapPanel.Children.Add(productStackPanel); buyProduct.Click += GetProduct_Click; } }