// call delete function for the product selected and set a confirmation box.
        private void btnDeleteProduct_Click(object sender, EventArgs e)
        {
            string selectedProduct = lbProducts.SelectedItem.ToString();
            var    confirmDelete   = MessageBox.Show("Are you sure you want to delete " + selectedProduct + "?", "Confirm?", MessageBoxButtons.YesNo);

            if (confirmDelete == DialogResult.Yes)
            {
                try
                {
                    ProductDB.DeleteProduct(selectedProduct);
                    MessageBox.Show(selectedProduct + " was deleted");
                }
                catch
                {
                    MessageBox.Show(selectedProduct + " could not be deleted, contact the administrator");
                }
                displayProducts();
                listProducts();
                cbProducts.SelectedIndex = -1;
                cbProducts.Text          = "";
            }
            else
            {
                MessageBox.Show(selectedProduct + " was not deleted");
            }
        }
示例#2
0
 private void btnDeleteProductClick_Click(object sender, EventArgs e)
 {
     if (delProduct != null)
     {
         DialogResult result = MessageBox.Show("Are you sure you want to delete " + delProduct.ProdName
                                               + " product ID " + delProduct.ProductID + "? ", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question,
                                               MessageBoxDefaultButton.Button2);
         if (result == DialogResult.Yes)
         {
             try
             {
                 ProductDB.DeleteProduct(delProduct);
                 products = ProductDB.GetProducts();
                 productDataGridView.DataSource = products;
                 delProduct = null;
             }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message, ex.GetType().ToString());
             }
         }
     }
     else
     {
         MessageBox.Show("Please select a product to delete", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }