private void btnSave_Click(object sender, EventArgs e) { if (IsValidData()) { try { // Create an instance of the Product class named product (lowercase) // using its pramerized constructor method. product = new Sailboat(txtCode.Text, txtDescription.Text, Convert.ToInt32(txtNumOfSails.Text), Convert.ToDecimal(txtPrice.Text)); // Close this form this.Close(); } catch (Exception ex) { MessageBox.Show("Exception " + ex.GetType().FullName + " thrown " + "\n" + "Message: " + ex.Message); } } }
public static List <Sailboat> GetProducts() { // create the list List <Sailboat> products = new List <Sailboat>(); // create the XmlReaderSettings object XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreWhitespace = true; settings.IgnoreComments = true; // create the XmlReader object XmlReader xmlIn = XmlReader.Create(Path, settings); // read past all nodes to the first Product node if (xmlIn.ReadToDescendant("Product")) { // create one Product object for each Product node do { Sailboat product = new Sailboat(); xmlIn.ReadStartElement("Product"); product.Code = xmlIn.ReadElementContentAsString(); product.OwnerName = xmlIn.ReadElementContentAsString(); product.NumOfSails = xmlIn.ReadElementContentAsInt(); product.Price = xmlIn.ReadElementContentAsDecimal(); products.Add(product); }while (xmlIn.ReadToNextSibling("Product")); } // close the XmlReader object xmlIn.Close(); return(products); }
private void btnDelete_Click(object sender, EventArgs e) { // *** TODO - Determine the index of the selected product to delete int i = lstProducts.SelectedIndex; // *** TODO - Check that the user did select a product if (i != -1) { // *** TODO - Create a product object and assign it the element // in the sailboat list that has the index value // identified in the first TODO of this event handler. // NOTE: The sailboat[] element will need to be cast // as a Product. Sailboat product = (Sailboat)sailboat[i]; string message = "Are you sure you want to delete " + product.OwnerName + "?"; // *** TODO - Declare a DialogResult object named button and // assign it the value returned from the MessageBox.Show() // method. The MessageBox should display the value of the message // (assigned in the previous statement) and a "Confirm Delete" caption, // and display a Yes or a No button. DialogResult button = MessageBox.Show(message, "Confirm Delete", MessageBoxButtons.YesNo); // *** TODO - Check to see if the button variable contrains a DialogResult of Yes if (button == DialogResult.Yes) { sailboat.Remove(product); SailboatDB.SaveProducts(sailboat); FillProductListBox(); } } }
private void btnAdd_Click(object sender, EventArgs e) { // *** TODO - Instantiate a new instance of the frmNewProducts() form. frmNewBoat newProductForm = new frmNewBoat(); // *** TODO - Instantiate a new Product object named product and assign it // the return value of the newProductsForm's GetNewProduct() method. Sailboat product = newProductForm.GetNewProduct(); // *** TODO - Check to see if the pervious statement returned a product. if (product != null) { // *** TODO - Use the Add method of the List<> collect to add the returned // product to the list. sailboat.Add(product); // *** TODO - Execute the static SaveProducts() method of the ProductDB class, // passing it the entire sailboat list. SailboatDB.SaveProducts(sailboat); // *** TODO - Call the function FillProductListBox(); FillProductListBox(); } }