private void btnAddBoMItem_Click(object sender, EventArgs e) { if (tbxAddBoMItem.Text != "") { // Create a new BoMItem BoMItem newBoMItem = new BoMItem(tbxAddBoMItem.Text, 1); // Get a list of the selected products List <Product> selectedProducts = lbxProducts.SelectedItems.Cast <Product>().ToList(); // Loop through each selected Product foreach (Product selectedProduct in selectedProducts) { bool added = false; // Loop through each BoMItem in each selected product foreach (BoMItem bomItem in selectedProduct.BomItems) { // If the new BoMItem matches any of the ones from the selected products // then increment it instead of adding it if (bomItem.ProductCode == newBoMItem.ProductCode) { bomItem.Quantity = Math.Max(0, bomItem.Quantity) + 1; added = true; } } // If the selected product doesn't already have the BoMItem, then add it if (!added) { selectedProduct.BomItems.Add(newBoMItem); } } } }
/// <summary> /// Loads a TradeVine BoM CSV into a list of Products /// </summary> /// <param name="fn">A string containing the filename of the CSV to load</param> /// <returns>A list of products extracted from the CSV file</returns> private List <Product> LoadCSV(string fn) { // Create a temporary list of products List <Product> newProducts = new List <Product>(); // Try to open the CSV try { // Open the file with a StreamReader StreamReader sr = new StreamReader(fn); // Loop through the file one line at a time while (!sr.EndOfStream) { // Read the line of text as a record string record = sr.ReadLine(); // Split the record into cells at the , string[] recordCells = record.Split(','); if (recordCells.Count() != 4) { MessageBox.Show("CSV File has the incorrect number of collumns. Please make sure the file has the following collumns:\n\nBoM Product Code\nBoM Component Product Code\nBoM Component Quantity\nBoM Remove", "Invalid format"); break; } // Ignore the header if (recordCells[0] != "BoM Product Code") { // Create a new BoMItem from the cells in the record BoMItem bomItem = new BoMItem(recordCells[1], (recordCells[3] == "Yes") ? -1 : Convert.ToInt32(recordCells[2])); // Placeholder variable that determines whether this BoMItem was added to an // existing product or needs to be added to a new one bool addToNew = true; // Loop through all of the products to see if the product this BoM item belongs // to already exists foreach (Product p in newProducts) { // Check the ProductCode with the first cell in the record if (p.ProductCode == recordCells[0]) { // If the product this BoMItem belongs to already exists, then add the // BoMItem to that product p.BomItems.Add(bomItem); // Indicate that we don't need to add this BoMItem to a new product addToNew = false; // Break the foreach loop as there should be only one of each product // in the list of new products break; } } // If the BoMItem needs to be added to a new product if (addToNew) { // Create the product Product newProduct = new Product(recordCells[0], new List <BoMItem>()); // Add the BoMItem to the new product newProduct.BomItems.Add(bomItem); // Add the new product to the newProducts list newProducts.Add(newProduct); } } } sr.Close(); currentFile = fn; } catch { MessageBox.Show("There was an error opening the file. Please make sure you have permission to access the file, and that the file is not already in use by another program"); } // Return the list of new products return(newProducts); }