Part 6. Adding and Editing products
The AddEditProduct form
The AddEditProduct form will be used to gather the data needed to create or update products. The form will work in either add or edit mode. Open the AddEditProduct form and switch to code view. Add the following using statement to the top of the page:
using HailstoneSoftware.QuickStartDAL;
Add the following line of code to the start of the AddEditProduct class. We need a global variable that represents the product that we are adding or editing.
// The product being added or edited
private Product currentProduct = null;
Next, we need to replace the AddEditProduct class constructor method with one that implements an add and edit mode:
public AddEditProduct(bool newProduct, Product product)
{
InitializeComponent();
// Keep hold of the product so we can update it if the user clicks Accept
this.currentProduct = product;
// Is this a new product?
if (newProduct)
{
// We want to run in new product mode
this.Text = "Add a new product";
this.mainTitleLabel.Text = "Add a new product";
}
else
{
// We want to run in edit product mode
this.Text = "Edit a product";
this.mainTitleLabel.Text = "Edit a product";
// Pre-populate the textboxes with the product values
this.productNameTextBox.Text = this.currentProduct.Name;
this.fullPriceTextBox.Text = this.currentProduct.FullPrice.ToString();
this.salePriceTextBox.Text = this.currentProduct.SalePrice.ToString();
this.descriptionTextBox.Text = this.currentProduct.Description;
}
}
In the replacement constructor, we pass in a product object. This object is either a new object or an existing object. Either way, if the user clicks the accept button we will populate the object using the data that the user entered on the form. The other parameter is a simple boolean that tells us whether we are creating a new product or editing an existing one. If we are editing an existing product then the product object passed in is used to pre-populate the fields on the form.
The Accept button
To complete the AddEditProduct form we will add code that is called when the user clicks the Accept button. We will populate the product object from the data that the user entered in the form. Add the following code to the Click event of the Accept button:
float fullPrice, salePrice;
// Validate the data entered
if (!float.TryParse(this.fullPriceTextBox.Text, out fullPrice))
{
// The user did not enter a float in the full price textbox
MessageBox.Show(this, "Please enter a numeric full price");
return;
}
if (!float.TryParse(this.salePriceTextBox.Text, out salePrice))
{
// The user did not enter a float in the sale price textbox
MessageBox.Show(this, "Please enter a numeric sale price");
return;
}
// Populate the product object using the details entered on this form
this.currentProduct.Name = this.productNameTextBox.Text;
this.currentProduct.FullPrice = fullPrice;
this.currentProduct.SalePrice = salePrice;
this.currentProduct.Description = this.descriptionTextBox.Text;
// Close the form
this.DialogResult = DialogResult.OK;
this.Close();
The code firstly checks that the user entered currency values in the full price and sale price textboxes. We then populate the global product object from the data that the user entered in the form. Lastly, we close the form.
Adding products
Next, we want to get the New Product button on the main form to show the AddEditProduct form that we've just coded. Open the MainForm and switch to code view. Add the following code to the Click event of the New Product button:
// Create a new product object
Product newProduct = new Product();
// Use the AddEditProduct dialog to populate the new product object
AddEditProduct addEditProduct = new AddEditProduct(true, newProduct);
// Show the AddEditProduct dialog
if (addEditProduct.ShowDialog(this) == DialogResult.OK)
{
// The user clicked the Accept button on the AddEditProduct dialog
// Add the new product to the data store
Product.Add(newProduct, this.dataStore);
// Populate the products listview
this.RefreshProductsListView();
}
The first thing we do when the user clicks the New Product button is create a new product object. At this point the new product object is blank and exists only in memory. Next, we display the AddEditProduct dialog, passing in our new product object and instructing the AddEditProduct dialog to run in add mode. Then we wait until the user clicks the Accept or Cancel button on the dialog.
If the user clicks the Accept button on the AddEditProduct dialog, we add the new product to the database. The AddEditProduct dialog will have populated the product object from the data that the user entered on the form. Lastly, we refresh the listview since there is now a new product object in the database.
Editing products
We also want to get the Edit Product button to show the AddEditProduct form. Add the following code to the Click event of the Edit Product button:
// Make sure a listview item is selected
if (this.productsListView.SelectedItems.Count > 0)
{
// Extract the product from the selected listview item
Product editProduct = new
Product((DataProduct)this.productsListView.SelectedItems[0].Tag);
// Use the AddEditProduct dialog to populate the new product object
AddEditProduct addEditProduct = new AddEditProduct(false, editProduct);
// Show the AddEditProduct dialog
if (addEditProduct.ShowDialog(this) == DialogResult.OK)
{
// The user clicked the Accept button on the AddEditProduct dialog
// Update the product in the data store
Product.Update(editProduct, this.dataStore);
// Populate the products listview
this.RefreshProductsListView();
}
}
We start out with a guard condition to prevent the code being executed when no listview items are selected. We will only deal with the first selected listview item. When we originally populated the listview, we set the Tag property of each row to the DataProduct object returned from the database. We firstly extract the DataProduct object from the Tag property of the first selected listview item and use it to populate a Product object. For more details on Data objects see the working with data structures page.
Next, we display the AddEditProduct dialog, passing in the existing product object that we just extracted from the selected listview item. We instruct the AddEditProduct dialog to run in edit mode. Then we wait until the user clicks the Accept or Cancel button on the dialog.
If the user clicks the Accept button on the AddEditProduct dialog, we update the existing product object in the database. The AddEditProduct dialog will have populated the product object from the data that the user entered on the form. Lastly, we refresh the listview since the data in the database has changed.