コード例 #1
0
            /// <summary>
            /// Adds a book to the shopping cart.
            /// </summary>
            /// <param name="book"></param>
            /// <param name="example"></param>
            public void AddItem(NBook book, NWidgetHostingExample example)
            {
                // try find an item with the same name. if such exists -> incerase quantity and rebuild shopping cart widget
                for (int i = 0; i < Items.Count; i++)
                {
                    if (Items[i].Name == book.Name)
                    {
                        Items[i].Quantity++;
                        example.m_ShoppingCartWidget.Content = CreateWidget(example);
                        return;
                    }
                }

                // add new item and rebuild shopping cart widget
                NShoppingCartItem item = new NShoppingCartItem();

                item.Name     = book.Name;
                item.Price    = book.Price;
                item.Quantity = 1;
                Items.Add(item);
                example.m_ShoppingCartWidget.Content = CreateWidget(example);
            }
コード例 #2
0
            /// <summary>
            /// Creates the widget that represents the current state of the shopping cart.
            /// </summary>
            /// <param name="example"></param>
            /// <returns></returns>
            public NWidget CreateWidget(NWidgetHostingExample example)
            {
                if (Items.Count == 0)
                {
                    return(new NLabel("The Shopping Cart is Empty"));
                }

                const double spacing = 10;

                // create the grand total label
                NLabel grandTotalLabel = new NLabel(GetGrandTotal().ToString("0.00"));

                // items table
                NTableFlowPanel tableFlowPanel = new NTableFlowPanel();

                tableFlowPanel.HorizontalSpacing = spacing;
                tableFlowPanel.VerticalSpacing   = spacing;
                tableFlowPanel.MaxOrdinal        = 5;

                // add headers
                tableFlowPanel.Add(new NLabel("Name"));
                tableFlowPanel.Add(new NLabel("Quantity"));
                tableFlowPanel.Add(new NLabel("Price"));
                tableFlowPanel.Add(new NLabel("Total"));
                tableFlowPanel.Add(new NLabel());

                for (int i = 0; i < Items.Count; i++)
                {
                    NShoppingCartItem item = Items[i];

                    // name
                    NLabel nameLabel  = new NLabel(item.Name);
                    NLabel priceLabel = new NLabel(item.Price.ToString("0.00"));
                    NLabel totalLabel = new NLabel(item.GetTotal().ToString("0.00"));

                    // quantity
                    NNumericUpDown quantityNud = new NNumericUpDown();
                    quantityNud.Value         = item.Quantity;
                    quantityNud.DecimalPlaces = 0;
                    quantityNud.Minimum       = 0;
                    quantityNud.ValueChanged += delegate(NValueChangeEventArgs args) {
                        item.Quantity        = (int)((NNumericUpDown)args.TargetNode).Value;
                        totalLabel.Text      = item.GetTotal().ToString("0.00");
                        grandTotalLabel.Text = GetGrandTotal().ToString("0.00");
                    };

                    NButton deleteButton = new NButton("Delete");
                    deleteButton.Click += delegate(NEventArgs args){
                        DeleteItem(item, example);
                    };

                    tableFlowPanel.Add(nameLabel);
                    tableFlowPanel.Add(quantityNud);
                    tableFlowPanel.Add(priceLabel);
                    tableFlowPanel.Add(totalLabel);
                    tableFlowPanel.Add(deleteButton);
                }

                // add grand total
                tableFlowPanel.Add(new NLabel("Grand Total"));
                tableFlowPanel.Add(new NLabel());
                tableFlowPanel.Add(new NLabel());
                tableFlowPanel.Add(grandTotalLabel);
                tableFlowPanel.Add(new NLabel());

                return(tableFlowPanel);
            }
コード例 #3
0
 public void DeleteItem(NShoppingCartItem item, NWidgetHostingExample example)
 {
     // remove the item and rebuild the shopping cart
     Items.Remove(item);
     example.m_ShoppingCartWidget.Content = CreateWidget(example);
 }