Пример #1
0
        private void btnCreateStoreItems_Click(object sender, EventArgs e)
        {
            StoreItem item = new StoreItem();
            StoreItems <StoreItem> items        = new StoreItems <StoreItem>();
            BinaryFormatter        bfmStoreItem = new BinaryFormatter();

            // This is the file that holds the list of items
            string strFileName = @"C:\Microsoft Visual C# Application Design\Computer Accessories Store\StoreItems.slm";

            items.Add(new StoreItem("7MP-00011", "Mouse", "Wireless", "Arc Touch Bluetooth Mouse ", 69.95));
            items.Add(new StoreItem("485948", "Mouse", "Wired", "Amazon Basic", 20));
            items.Add(new StoreItem("920820", "Mouse", "Wireless", "Microsoft ergonomics", 80));
            items.Add(new StoreItem("406033", "Mouse", "Wired", "   Logitech", 45));
            items.Add(new StoreItem("358460", "Keyboard", "Wireless", "Logitech All-in-one", 50));
            items.Add(new StoreItem("724799", "Monitor", "1080p", "LG", 200));
            items.Add(new StoreItem("582693", "Thumbdrive", "USB 3.0", "Moserbaer", 15));
            items.Add(new StoreItem("350250", "Keyboard", "Wired", "Hp", 30));
            items.Add(new StoreItem("332085", "Thumbdrive", "USB 2.0", "SanDisk", 10));
            items.Add(new StoreItem("836360", "Thumbdrive", "USB 3.0", "SONY", 9));

            // Save the StoreItems collection
            using (FileStream stmStoreItem = new FileStream(strFileName,
                                                            FileMode.Create,
                                                            FileAccess.Write,
                                                            FileShare.Write))
            {
                bfmStoreItem.Serialize(stmStoreItem, items);
            }
        }
Пример #2
0
        private void btnNewStoreItem_Click(object sender, EventArgs e)
        {
            lbxSubCategories.Items.Clear();
            lvwAvailableItems.Items.Clear();
            NewStoreItem nsi = new NewStoreItem();

            StoreItem item = new StoreItem();
            StoreItems <StoreItem> items        = new StoreItems <StoreItem>();
            BinaryFormatter        bfmStoreItem = new BinaryFormatter();

            // This is the file that holds the list of items
            string strFileName = @"C:\Microsoft Visual C# Application Design\Computer Accessories Store\StoreItems.slm";

            if (nsi.ShowDialog() == DialogResult.OK)
            {
                // Make sure the user had selected a category
                if (string.IsNullOrEmpty(nsi.cbxCategories.Text))
                {
                    MessageBox.Show("You must specify the item's category.",
                                    "Computer Accessories Store",
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                // Make sure the user had entered a name/description
                if (string.IsNullOrEmpty(nsi.txtItemName.Text))
                {
                    MessageBox.Show("You must enter the name (or a " +
                                    "short description) for the item.",
                                    "Computer Accessories Store",
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                // Make sure the user had typed a price for the item
                if (string.IsNullOrEmpty(nsi.txtUnitPrice.Text))
                {
                    MessageBox.Show("You must enter the price of the item.",
                                    "Computer Accessories Store",
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                // Before saving the new item, find out if there was
                // already a file that holds the list of items
                // If that file exists, open it and store its items
                // in our StoreItems list
                if (File.Exists(strFileName))
                {
                    using (FileStream stmStoreItem = new FileStream(strFileName,
                                                                    FileMode.Open,
                                                                    FileAccess.Read,
                                                                    FileShare.Read))
                    {
                        // Retrieve the list of items from file
                        items = (StoreItems <StoreItem>)bfmStoreItem.Deserialize(stmStoreItem);
                    }
                }

                // Create the accessory item
                item.ItemNumber  = nsi.txtItemNumber.Text;
                item.Category    = nsi.cbxCategories.Text;
                item.SubCategory = nsi.cbxSubCategories.Text;
                item.ItemName    = nsi.txtItemName.Text;
                item.UnitPrice   = double.Parse(nsi.txtUnitPrice.Text);

                // Call the Add method of our collection class to add the item
                items.Add(item);

                // Save the StoreItems collection
                using (FileStream stmStoreItem = new FileStream(strFileName,
                                                                FileMode.Create,
                                                                FileAccess.Write,
                                                                FileShare.Write))
                {
                    bfmStoreItem.Serialize(stmStoreItem, items);
                }
            }

            InitializeStoreItems();
        }