Пример #1
0
        private void RefreshAll(bool refreshButtonPressed = false)
        {
            // Hide messages
            if (refreshButtonPressed)
            {
                messageLabel.Hide();
            }

            // Refresh shop inventory
            Product[] products = client.GetProducts();

            // Get name from selected item before refresh
            string selectedName = "";

            if (shopListBox.ValueMember != null)
            {
                var selected = shopListBox.SelectedValue;
                if (selected != null)
                {
                    Product selectedProduct = (Product)selected;
                    selectedName = selectedProduct.Name;
                }
            }

            // refresh list
            if (products.Length > 0)
            {
                var labels = from p in products
                             select $"{p.Name}: €{p.Price} - {p.Qnty} in stock";

                var dict = products.Zip(labels.ToArray(), (p, s) => new { p, s })
                           .ToDictionary(item => item.p, item => item.s);

                shopListBox.DataSource    = new BindingSource(dict, null);
                shopListBox.DisplayMember = "Value";
                shopListBox.ValueMember   = "Key";
            }
            else
            {
                shopListBox.DataSource    = null;
                shopListBox.DisplayMember = null;
                shopListBox.ValueMember   = null;
            }

            // Select last selected item if still available
            if (!String.IsNullOrEmpty(selectedName))
            {
                for (int i = 0; i < shopListBox.Items.Count; i++)
                {
                    KeyValuePair <Product, string> item = (KeyValuePair <Product, string>)shopListBox.Items[i];
                    if (item.Key.Name == selectedName)
                    {
                        shopListBox.SelectedIndex = i;
                    }
                }
            }

            // Refresh customer inventory
            products = client.GetCart(customer);
            if (products.Length > 0)
            {
                var labels = from p in products
                             select $"{p.Name}, owned: {p.Qnty}";

                var dict = products.Zip(labels.ToArray(), (p, s) => new { p, s })
                           .ToDictionary(item => item.p, item => item.s);

                ownListBox.DataSource    = new BindingSource(dict, null);
                ownListBox.DisplayMember = "Value";
                ownListBox.ValueMember   = "Key";
            }

            // MoneyLabel
            customer.Credit = client.GetCredit(customer);
            moneyLabel.Text = $"Money left: €{customer.Credit}";
        }