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); } }
private void btnFirstItem_Click(object sender, EventArgs e) { BinaryFormatter bfmStoreItem = new BinaryFormatter(); StoreItems <StoreItem> items = new StoreItems <StoreItem>(); // This is the file that holds the list of items string strFileName = @"C:\Microsoft Visual C# Application Design\Computer Accessories Store\StoreItems.slm"; 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); StoreItem item = items.First(sc => sc.SubCategory == "Electric Guitars"); MessageBox.Show("Item #:\t\t" + item.ItemNumber + "\n" + "Category:\t" + item.Category + "\n" + "Sub-Category:\t" + item.SubCategory + "\n" + "Item Name:\t" + item.ItemName + "\n" + "Unit Price:\t" + item.UnitPrice.ToString("C"), "Computer Accessories Store", MessageBoxButtons.OK, MessageBoxIcon.Information); } } }
private void btnDeleteAllStoreItems_Click(object sender, EventArgs e) { BinaryFormatter bfmStoreItem = new BinaryFormatter(); StoreItems <StoreItem> items = new StoreItems <StoreItem>(); string strFileName = @"C:\Microsoft Visual C# Application Design\Computer Accessories Store\StoreItems.slm"; if (MessageBox.Show("Are you sure you want to delete all items?", "Computer Accessories Store", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes) { items.Clear(); MessageBox.Show("All items have been deleted from the inventory.", "Computer Accessories Store", MessageBoxButtons.OK, MessageBoxIcon.Information); lvwAvailableItems.Items.Clear(); lbxSubCategories.Items.Clear(); lbxCategories.Items.Clear(); if (File.Exists(strFileName)) { File.Delete(strFileName); } } }
private void InitializeStoreItems() { lbxCategories.Items.Clear(); lbxSubCategories.Items.Clear(); lvwAvailableItems.Items.Clear(); StoreItems <StoreItem> items = new StoreItems <StoreItem>(); BinaryFormatter bfmStoreItem = new BinaryFormatter(); string strFileName = @"C:\Microsoft Visual C# Application Design\Computer Accessories Store\StoreItems.slm"; 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); allStoreItems = items; // Display the categories in the combo box foreach (StoreItem item in items) { if (!lbxCategories.Items.Contains(item.Category)) { lbxCategories.Items.Add(item.Category); } } pbxSelectedItem.Image = Image.FromFile(@"C:\Microsoft Visual C# Application Design\Item.jpg"); } } }
// Done private void btnFind_Click(object sender, EventArgs e) { 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 (string.IsNullOrEmpty(txtItemNumber.Text)) { MessageBox.Show("You must enter an item number.", "Computer Accesories Store", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } 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 item in items) { if (item.ItemNumber == txtItemNumber.Text) { cbxCategories.Text = item.Category; cbxSubCategories.Text = item.SubCategory; txtItemName.Text = item.ItemName; txtUnitPrice.Text = item.UnitPrice.ToString("F"); } } } } strFileName = @"C:\Microsoft Visual C# Application Design\" + txtItemNumber.Text + ".jpg"; if (File.Exists(strFileName)) { pbxSelectedItem.Image = Image.FromFile(strFileName); } else { pbxSelectedItem.Image = Image.FromFile(@"C:\Microsoft Visual C# Application Design\Item.jpg"); } }
// Done private void cbxCategories_SelectedIndexChanged(object sender, EventArgs e) { 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 Accesories Store\StoreItems.slm"; // If the file for the store inventory was created already, ... if (File.Exists(strFileName)) { // ... open it 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); // Display the categories in the combo box for (int i = 0; i < items.Count; i++) { // Get store item based on its index StoreItem item = (StoreItem)items[i]; // Get the item category if (item.Category == cbxCategories.Text) { // Make sure the category is not yet in the Categories combo box if (!cbxSubCategories.Items.Contains(item.SubCategory)) { cbxSubCategories.Items.Add(item.SubCategory); } } } } } }
// Done private void NewStoreItem_Load(object sender, EventArgs e) { 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 (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); // Display the categories in the combo box for (int i = 0; i < items.Count; i++) { StoreItem item = (StoreItem)items[i]; if (!cbxCategories.Items.Contains(item.Category)) { cbxCategories.Items.Add(item.Category); } } } } strFileName = @"C:\Microsoft Visual C# Application Design\Item.jpg"; if (File.Exists(strFileName)) { pbxSelectedItem.Image = Image.FromFile(strFileName); } }
// Done private void lbxSubCategories_SelectedIndexChanged(object sender, EventArgs e) { lvwAvailableItems.Items.Clear(); BinaryFormatter bfmStoreItem = new BinaryFormatter(); StoreItems <StoreItem> items = new StoreItems <StoreItem>(); // This is the file that holds the list of items string strFileName = @"C:\Microsoft Visual C# Application Design\Computer Accessories Store\StoreItems.slm"; 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); // Display the sub-categories in the combo box foreach (StoreItem item in items) { // Get only the sub-categories of the selected category if (item.SubCategory == lbxSubCategories.SelectedItem.ToString()) { ListViewItem lviStoreItem = new ListViewItem(item.ItemNumber); lviStoreItem.SubItems.Add(item.ItemName); lviStoreItem.SubItems.Add(item.UnitPrice.ToString("F")); lvwAvailableItems.Items.Add(lviStoreItem); } } } } }
// Done private void btnMaintain_Click(object sender, EventArgs e) { bool itemFound = false; BinaryFormatter bfmStoreItem = new BinaryFormatter(); StoreItems <StoreItem> items = new StoreItems <StoreItem>(); // This is the file that holds the list of items string strFileName = @"C:\Microsoft Visual C# Application Design\Computer Accessories Store\StoreItems.slm"; // Make sure the user entered a valid item number. Otherwise, don't do nothing if (string.IsNullOrEmpty(txtItemNumber.Text)) { MessageBox.Show("You must enter an item number.", "Computer Accessories Store", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } // Make sure the file that holds the store inventory was created already if (File.Exists(strFileName)) { // If the inventory file exists, open it 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); // Because we are using one dialog box for update and delete operations, // first find out what operation is being conducted. // If the user is trying to update a store item... if (btnMaintain.Text == "Update Store Item") { // ... as a courtesy (just in case), ask the user to confirm the operation if (MessageBox.Show("Are you sure you want to update this item?", "Computer Accessories Store", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes) { // Now that we know that the user wants to update the item, locate it foreach (StoreItem item in items) { // If you find an item that has the number in the Item # text box, ... if (item.ItemNumber == txtItemNumber.Text) { // ... change the values of that item based on those on the dialog box item.Category = cbxCategories.Text; item.SubCategory = cbxSubCategories.Text; item.ItemName = txtItemName.Text; item.UnitPrice = double.Parse(txtUnitPrice.Text); // Since the item was found, make a note itemFound = true; // Now that the item has been found, stop looking for it break; } } // As a courtesy, let the user know that the item was updated MessageBox.Show("The item has been updated in the inventory.", "Computer Accessories Store", MessageBoxButtons.OK, MessageBoxIcon.Question); } } else // if (btnMaintain.Text == "Delete Store Item") { // Make sure the user really wants to delete the item if (MessageBox.Show("Are you sure you want to delete this item?", "Computer Accessories Store", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes) { // Since the user really wants to delete the item, look for it foreach (StoreItem item in items) { // If you find the item, ... if (item.ItemNumber == txtItemNumber.Text) { // ... remove it from the inventory items.Remove(item); // Let the user know that the item was deleted MessageBox.Show("The item has been removed from the inventory.", "Computer Accessories Store", MessageBoxButtons.OK, MessageBoxIcon.Question); // Since the item was found and deleted, make a note itemFound = true; // Stop looking for the item break; } } } } } // Since there have been changes in the inventory (the StoreItems collection), update the file using (FileStream stmStoreItem = new FileStream(strFileName, FileMode.Create, FileAccess.Write, FileShare.Write)) { bfmStoreItem.Serialize(stmStoreItem, items); } } // If the item was updated or deleted, close the dialog box if (itemFound == true) { Close(); } }
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(); }