// Precondition: Address list cannot be empty // Postcondition: An address object is selected and can be edited by the user private void addressesToolStripMenuItem_Click(object sender, EventArgs e) { ChooseAddressForm chooseAddressForm; // the choose address dialog box form DialogResult result1; // holds the result from the dialog box if (addressList.Count < 1) // ensures the address list is not empty { MessageBox.Show("There are no addresses to edit", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { chooseAddressForm = new ChooseAddressForm(addressList);// creates form and sends the address list result1 = chooseAddressForm.ShowDialog(); if (result1 == DialogResult.OK) { int index = chooseAddressForm.AddressIndex; // holds selection index Address a = addressList[index]; // variable to hold selected address AddressForm addressForm = new AddressForm(); // creates address form dialog box // sets text boxes on form to the properties from the selected address addressForm.AddressName = a.Name; addressForm.Address1 = a.Address1; addressForm.Address2 = a.Address2; addressForm.City = a.City; addressForm.State = a.State; addressForm.ZipText = a.Zip.ToString(); DialogResult result2 = addressForm.ShowDialog(); if (result2 == DialogResult.OK) { // sets the properties for the selected address to the edited values a.Name = addressForm.AddressName; a.Address1 = addressForm.Address1; a.Address2 = addressForm.Address2; a.City = addressForm.City; a.State = addressForm.State; a.Zip = int.Parse(addressForm.ZipText); } addressForm.Dispose(); } chooseAddressForm.Dispose(); } }
// Precondition: Insert, Address menu item activated // Postcondition: The Address dialog box is displayed. If data entered // are OK, an Address is created and added to the list // of addresses private void addressToolStripMenuItem_Click(object sender, EventArgs e) { AddressForm addressForm = new AddressForm(); // The address dialog box form DialogResult result = addressForm.ShowDialog(); // Show form as dialog and store result if (result == DialogResult.OK) // Only add if OK { try { Address newAddress = new Address(addressForm.AddressName, addressForm.Address1, addressForm.Address2, addressForm.City, addressForm.State, int.Parse(addressForm.ZipText)); // Use form's properties to create address addressList.Add(newAddress); } catch (FormatException) // This should never happen if form validation works! { MessageBox.Show("Problem with Address Validation!", "Validation Error"); } } addressForm.Dispose(); // Best practice for dialog boxes }
// Precondition: Edit, Address menu item activated // Postcondition: The address selected has been edited with the new information replacing // the existing object's properties private void addressToolStripMenuItem1_Click(object sender, EventArgs e) { List<Address> Addresses; Addresses = addressList; if ((Addresses.Count() == 0)) MessageBox.Show("Must have address to edit", "Edit Error"); else { SelectAddressForm saForm = new SelectAddressForm(Addresses); DialogResult result = saForm.ShowDialog(); if (result == DialogResult.OK) { int editIndex; editIndex = saForm.addressIndex; if (editIndex >= 0) { Address editAddress = Addresses[editIndex]; AddressForm addressForm = new AddressForm(); addressForm.AddressName = editAddress.Name; DialogResult editResult = addressForm.ShowDialog(); if (editResult == DialogResult.OK) { editAddress.Name = addressForm.AddressName; } addressForm.Dispose(); } } saForm.Dispose(); } }
// Preconditions: Insert address is clicked // Postconditions: Address is created private void addressToolStripMenuItem_Click(object sender, EventArgs e) { AddressForm addressForm = new AddressForm(); DialogResult result = addressForm.ShowDialog(); Address myAddress; if(result==DialogResult.OK) { addresses.Add(addressForm.name, addressForm.addressLine1, addressForm.addressLine2, addressForm.city, addressForm.stateIndex.ToString(), addressForm.zip); } addressForm.Dispose(); }