//Precondition: User clicks Edit > Address //Postcondition: ChooseAddressForm is displayed private void addressToolStripMenuItemOne_Click(object sender, EventArgs e) { ChooseAddressForm chooseForm = new ChooseAddressForm(upv.AddressList); //Send list of addresses DialogResult result = chooseForm.ShowDialog(); //Result is shown int addressIndex; //Used for index position if (result == DialogResult.OK) { addressIndex = chooseForm.chosenIndex; //Inherit index from ChooseAddressForm AddressForm addressEdit = new AddressForm(); // Opens new address form to edit selected address //Fill in form with selected address addressEdit.AddressName = upv.AddressList[addressIndex].Name; addressEdit.Address1 = upv.AddressList[addressIndex].Address1; addressEdit.Address2 = upv.AddressList[addressIndex].Address2; addressEdit.City = upv.AddressList[addressIndex].City; addressEdit.State = upv.AddressList[addressIndex].State; addressEdit.ZipText = upv.AddressList[addressIndex].Zip.ToString(); DialogResult editResult = addressEdit.ShowDialog(); //Stores result from addressEdit //Fill in form with selected address if (editResult == DialogResult.OK) { upv.AddressList[addressIndex].Name = addressEdit.AddressName; upv.AddressList[addressIndex].Address1 = addressEdit.Address1; upv.AddressList[addressIndex].Address2 = addressEdit.Address2; upv.AddressList[addressIndex].City = addressEdit.City; upv.AddressList[addressIndex].State = addressEdit.State; upv.AddressList[addressIndex].Zip = Convert.ToInt32(addressEdit.ZipText); } } }
private void addressToolStripMenuItem1_Click(object sender, EventArgs e) { ChooseAddressForm chooseAddressForm; // The chooseAddress dialog box form DialogResult result; // The result of showing form as dialog if (upv.AddressCount < ChooseAddressForm.MIN_ADDRESSES) // Make sure we have enough addresses { MessageBox.Show("Need " + ChooseAddressForm.MIN_ADDRESSES + " address to edit address!", "Addresses Error"); return; // Exit now since can't edit valid address } chooseAddressForm = new ChooseAddressForm(upv.AddressList); // Send list of addresses int index; // Creates index to hold index place as int result = chooseAddressForm.ShowDialog(); // Sets result = dialog box of chooseAddressForm Address address; // Creates object address of type Address if (result == DialogResult.OK) // Only add if OK { index = chooseAddressForm.AddressIndex; // Address to be inserted address = upv.AddressAt(index); // gives address object the address at specified index AddressForm addressForm; // Creates addressForm of type AddressForm DialogResult dialogResult; // creates dialogResult of type DialogResult addressForm = new AddressForm(); // Create a new instance of the AddressForm //Loads in the various address properties 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 = addressForm.ShowDialog(); // Sets dialogResult = to loaded properties if (dialogResult == DialogResult.OK) //OK dialog result dpes the same load but now in reverse { 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); } } else // This should never happen if form validation works! { MessageBox.Show("Problem with Address Validation!", "Validation Error"); } chooseAddressForm.Dispose(); // Best practice for dialog boxes // Alternatively, use with using clause as in Ch. 17 }
// Precondition: Edit, Address menu item activated // Postcondition: The address selected from the list has been edited // with the new information replacing the existing object's // properties private void addressToolStripMenuItem1_Click(object sender, EventArgs e) { if (upv.AddressList.Count > 0) // Only edit if there are addresses! { ChooseAddressForm chooseAddForm = new ChooseAddressForm(upv.AddressList); // The choose address dialog box form DialogResult result = chooseAddForm.ShowDialog(); // Show form as dialog and store result if (result == DialogResult.OK) // Only edit if OK { int editIndex; // Index of address to edit editIndex = chooseAddForm.AddressIndex; if (editIndex >= 0) // -1 if didn't select item from combo box { Address editAddress = upv.AddressAt(editIndex); // The address being edited AddressForm addressForm = new AddressForm(); // The address dialog box form // Populate form fields from selected address addressForm.AddressName = editAddress.Name; addressForm.Address1 = editAddress.Address1; addressForm.Address2 = editAddress.Address2; addressForm.City = editAddress.City; addressForm.State = editAddress.State; addressForm.ZipText = $"{editAddress.Zip:D5}"; result = addressForm.ShowDialog(); // Show form as dialog and store result if (result == DialogResult.OK) // Only edit if OK { // Edit address properties using form fields editAddress.Name = addressForm.AddressName; editAddress.Address1 = addressForm.Address1; editAddress.Address2 = addressForm.Address2; editAddress.City = addressForm.City; editAddress.State = addressForm.State; if (int.TryParse(addressForm.ZipText, out int zip)) { editAddress.Zip = zip; } else { MessageBox.Show("Problem with Zip Validation!", "Validation Error"); } } addressForm.Dispose(); // Best practice for dialog boxes } } chooseAddForm.Dispose(); // Best practice for dialog boxes } else { MessageBox.Show("No addresses to edit!", "No Addresses"); } }
// Precondition: Edit, Addresses menu item activated // Postcondition: Allows you to edit an addresses opened from // the upv object that you selected in ChooseAddressForm private void addressesToolStripMenuItem_Click(object sender, EventArgs e) { ChooseAddressForm chooseForm; // new instnace of ChooseAddressForm DialogResult result; // new dialogresult chooseForm = new ChooseAddressForm(upv.AddressList); // Send list of addresses result = chooseForm.ShowDialog(); // show chooseform dialog box if (result == DialogResult.OK) // Only add if OK { try { AddressForm addressForm = new AddressForm(); // new instance of address Address myAddress; // The address being added DialogResult resultTwo; // second dialog result myAddress = upv.AddressAt(chooseForm.AddressIndex); // get address from selected index and //assign it to variable myAddress addressForm.AddressName = myAddress.Name; // load address information into textbox addressForm.Address1 = myAddress.Address1; // load address information into textbox addressForm.Address2 = myAddress.Address2; // load address information into textbox addressForm.City = myAddress.City; // load address information into textbox addressForm.State = myAddress.State; // load address information into textbox addressForm.ZipText = myAddress.Zip.ToString("D5"); // load address information into textbox resultTwo = addressForm.ShowDialog(); // show form with preloaded address to edit if (resultTwo == DialogResult.OK) // Only add if OK { myAddress.Name = addressForm.AddressName; // Change address at selected index with new information myAddress.Address1 = addressForm.Address1; // Change address at selected index with new information myAddress.Address2 = addressForm.Address2; // Change address at selected index with new information myAddress.City = addressForm.City; // Change address at selected index with new information myAddress.State = addressForm.State; // Change address at selected index with new information myAddress.Zip = int.Parse(addressForm.ZipText); // Change address at selected index with new information } } catch (FormatException) // This should never happen if form validation works { MessageBox.Show("Problem with Letter Validation!", "Validation Error"); // if something wrong, show error } } }
// Precondition: Edit, Address menu item activated // Postcondition: Chosen address is updated according // to user's input private void editAddressToolStripMenuItem1_Click(object sender, EventArgs e) { ChooseAddressForm chooseAddressForm = new ChooseAddressForm(upv.AddressList); // The address dialog box form DialogResult chooseAddressResult = chooseAddressForm.ShowDialog(); // Show form as dialog and store result if (chooseAddressResult == DialogResult.OK) // Only select if OK { int addressAt = chooseAddressForm.ChooseAddressIndex; // Holds index for for the addressAt variable Address editAddress = upv.AddressAt(addressAt); // Holds address data AddressForm editAddressForm = new AddressForm(); // The address dialog box form editAddressForm.AddressName = editAddress.Name; editAddressForm.Address1 = editAddress.Address1; editAddressForm.Address2 = editAddress.Address2; editAddressForm.City = editAddress.City; editAddressForm.State = editAddress.State; editAddressForm.ZipText = editAddress.Zip.ToString("d5"); DialogResult editAddressResult = editAddressForm.ShowDialog(); // Show form as dialog and store result if (editAddressResult == DialogResult.OK) // Only edit if OK { upv.AddressAt(addressAt).Name = editAddressForm.AddressName; upv.AddressAt(addressAt).Address1 = editAddressForm.Address1; upv.AddressAt(addressAt).Address2 = editAddressForm.Address2; upv.AddressAt(addressAt).City = editAddressForm.City; upv.AddressAt(addressAt).State = editAddressForm.State; try { upv.AddressAt(addressAt).Zip = int.Parse(editAddressForm.ZipText); } catch (ArgumentOutOfRangeException) { // notify user if zip code is invalid MessageBox.Show("Enter valid zip code", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (ArgumentNullException) { // notify user if zip code is invalid MessageBox.Show("Enter a zip code", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (OverflowException) { // notify user if zip code is invalid MessageBox.Show("Enter valid zip code", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (FormatException) { // notify user if zip code is invalid MessageBox.Show("Enter valid zip code", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information); } } editAddressForm.Dispose(); } chooseAddressForm.Dispose(); }