//precondition: the address toolstrip menu item is clicked. //postcondition: the selected upv address object is updated private void AddressToolStripMenuItem1_Click(object sender, EventArgs e) { const int MINIMUM_ADDRESSES = 0; //the minimum addresses required to be able to edit //checks if there is at least one address //If yes, opens an EditAddress form. If no, shows a messagebox with error. if (upv.AddressCount > MINIMUM_ADDRESSES) { EditAddress editAddress = new EditAddress(upv.AddressList); // Send list of addresses and open EditAddress form DialogResult result = editAddress.ShowDialog(); //the dialog result of the EditAddress form int selectedIndex; //keeps track of selected indoex in the editaddress form. if (result == DialogResult.OK) // Only add if OK { selectedIndex = editAddress.AddressBoxIndex; //sets selectedIndex to the selected one in the EditAddress form. //Populates the address form textboxes with the selected address' data AddressForm addressForm = new AddressForm { AddressName = upv.AddressList[selectedIndex].Name, Address1 = upv.AddressList[selectedIndex].Address1, Address2 = upv.AddressList[selectedIndex].Address2, City = upv.AddressList[selectedIndex].City, State = upv.AddressList[selectedIndex].State, ZipText = upv.AddressList[selectedIndex].Zip.ToString() }; DialogResult editResult = addressForm.ShowDialog(); //sets editResult depending on what was clicked //checks if the result was ok. //If so, tries to update the upv address with the new information if (editResult == DialogResult.OK) { //tries to parse the zipcode. If successful, updates the upv address. if (int.TryParse(addressForm.ZipText, out int zip)) { upv.AddressList[selectedIndex].Name = addressForm.AddressName; upv.AddressList[selectedIndex].Address1 = addressForm.Address1; upv.AddressList[selectedIndex].Address2 = addressForm.Address2; upv.AddressList[selectedIndex].City = addressForm.City; upv.AddressList[selectedIndex].State = addressForm.State; upv.AddressList[selectedIndex].Zip = zip; } else // If there was an error { MessageBox.Show("Problem with Address Validation!", "Validation Error"); } } addressForm.Dispose(); //disposes the form } editAddress.Dispose(); // Disposes the form } else { //shows if there is nothing to edit MessageBox.Show("No addresses to edit!", "Addresses Error"); this.DialogResult = DialogResult.Abort; // Dismiss immediately } }
// Precondition: Edit, Address menu item activated // Postcondition: Dialog to seect address is shown, if user selects address // and clicks OK address dialog is shown to edit address info private void addressToolStripMenuItem1_Click(object sender, EventArgs e) { EditAddress editAddress = new EditAddress(upv.addresses); //edit addrss form passes addresses DialogResult result = editAddress.ShowDialog(); //stores the result of the editaddress dialog if (result == DialogResult.OK) //if the user clicks ok { Address address = upv.AddressAt(editAddress.AddressIndex); //stores the address the user selected to be edited AddressForm addressForm = new AddressForm(); //new instance of the address form //Populate the address text boxes with the info to be edited addressForm.AddressName = address.Name; addressForm.Address1 = address.Address1; addressForm.Address2 = address.Address2; addressForm.City = address.City; addressForm.State = address.State; addressForm.ZipText = address.Zip.ToString(); DialogResult editresult = addressForm.ShowDialog(); //stores the result of the address dialog if (editresult == DialogResult.OK) //if user clicked ok { //reassigns the addresses properties to the contents of the address forms fields address.Name = addressForm.AddressName; address.Address1 = addressForm.Address1; address.Address2 = addressForm.Address2; address.City = addressForm.City; address.State = addressForm.State; address.Zip = int.Parse(addressForm.ZipText); } addressForm.Dispose(); //dispose of the address form } editAddress.Dispose(); //dispose of the edit address form }