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(); }
private void btnReplaceItem_Click(object sender, EventArgs e) { StoreItem selected = null; StoreItem item = new StoreItem(); NewStoreItem nsi = new NewStoreItem(); BinaryFormatter bfmStoreItem = new BinaryFormatter(); StoreItems <StoreItem> items = new StoreItems <StoreItem>(); string strFileName = @"C:\Microsoft Visual C# Application Design\Computer Accessories Store\StoreItems.slm"; if (lvwAvailableItems.SelectedItems.Count == 0) { MessageBox.Show("No item is selected.", "Computer Accessories Store", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } else { 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); foreach (StoreItem itm in items) { if (itm.ItemNumber == lvwAvailableItems.SelectedItems[0].Text) { selected = itm; break; } } } } 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; } // Create the 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 replace the item items.Replace(selected, item); // Save the StoreItems collection using (FileStream stmStoreItem = new FileStream(strFileName, FileMode.Create, FileAccess.Write, FileShare.Write)) { bfmStoreItem.Serialize(stmStoreItem, items); } } } InitializeStoreItems(); }