// Save button for the products tab private void btnSaveProduct_Click(object sender, EventArgs e) { string selectedProd = lstProductList.GetItemText(lstProductList.SelectedItem); string editedProd = txtModifyProduct.Text; // Validation to see if the typed product is already in the list int index = lstProductList.FindString(editedProd, -1); if (editedProd != "" && index != -1) { MessageBox.Show("Warning: Product already exists.", "Duplicate Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { // Test for saving if (lstProductList.Enabled == false && editedProd != "") // Test if adding product { DialogResult result = MessageBox.Show("Save '" + editedProd + "' as a new product?", "Confirm Add", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation); if (result == DialogResult.OK) { ProductDB.AddProductName(editedProd); this.frmModifyProductSupplier_Load(this, null); // Select the item that was just saved SelectItem(editedProd, lstProductList); } } else if (lstProductList.Enabled == false && editedProd == "") // If adding product, check if textbox is empty { MessageBox.Show("Please enter a new product name.", "Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); txtModifyProduct.Focus(); } else if (lstProductList.Enabled == true && selectedProd != editedProd) // Test if editing a selected product { DialogResult update = MessageBox.Show("Change '" + selectedProd + "' to '" + editedProd + "'?" , "Confirm Change", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); if (update == DialogResult.OK) { ProductDB.UpdateProduct(selectedProd, editedProd); // Refresh table adapter to display updated list this.frmModifyProductSupplier_Load(this, null); // Select the item that was just updated SelectItem(editedProd, lstProductList); } } else if (lstProductList.Enabled == true && selectedProd == "") // Test if editing a product but nothing is selected { MessageBox.Show("Please select a product to edit.", "Item Not Selected", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); txtModifyProduct.Focus(); } } }