private void button1_Click(object sender, EventArgs e) { try { if (MessageBox.Show("Are you sure to add a product?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { name = textBoxName.Text; quantity = double.Parse(textBoxQuantity.Text); price = double.Parse(textBoxPrice.Text); pro = new Product(name, quantity, price); if (pro.repOK()) { proMan = new ProductManager(); proMan.saveProduct(pro); //write text file //MessageBox.Show(cus.toString()); DateTime now = DateTime.Now; System.IO.StreamWriter file = new System.IO.StreamWriter(@"property.txt", true); file.WriteLine("Add Product{" + pro.toString() + "}" + " " + now); file.Close(); tableGetData(); MessageBox.Show("Add Product successfully."); } else { MessageBox.Show("Invalid Product."); } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
private void button3_Click(object sender, EventArgs e) { try { if (MessageBox.Show("Are you sure to delete a product?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { int selectedIndex = table1.CurrentCell.RowIndex; if (selectedIndex >= 0) { id = table1.Rows[selectedIndex].Cells[0].Value.ToString(); proMan = new ProductManager(); proMan.deleteProduct(id); //write text file pro = new Product(); DateTime now = DateTime.Now; System.IO.StreamWriter file = new System.IO.StreamWriter(@"property.txt", true); file.WriteLine("Delete Product{" + id + "}" + " " + now); file.Close(); tableGetData(); MessageBox.Show("Delete Product successfully."); } else { MessageBox.Show("Select a product to delete."); } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }