//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: 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: None //Postcondition: Shows form for and creates address private void addressToolStripMenuItem_Click(object sender, EventArgs e) { AddressForm afrm = new AddressForm(); DialogResult result = afrm.ShowDialog(); //Shows address dialog box if (result == DialogResult.OK) { upv.AddAddress(afrm.NameValue, afrm.AddressOne, afrm.AddressTwo, afrm.City, afrm.StateIndex, afrm.Zip); } afrm.Dispose(); //Resources are released }
//Precondition: Edit, Address menu item activated. //Postcondition: A dialog box to choose an address to edit appears. Then an address form //with the chosen adress information appears. private void addressToolStripMenuItem1_Click(object sender, EventArgs e) { List <Address> Addresses; Addresses = upv.AddressList; if ((Addresses.Count() == 0)) { MessageBox.Show("Must have address to edit", "Edit Error"); } else { SelectAddressForm saForm = new SelectAddressForm(upv.AddressList); DialogResult result = saForm.ShowDialog(); if (result == DialogResult.OK) { int editIndex; editIndex = saForm.EditAddressIndex; if (editIndex >= 0) { Address editAddress = Addresses[editIndex]; AddressForm addressForm = new AddressForm(); addressForm.AddressName = editAddress.Name; addressForm.Address1 = editAddress.Address1; addressForm.Address2 = editAddress.Address2; addressForm.City = editAddress.City; addressForm.State = editAddress.State; addressForm.ZipText = editAddress.Zip.ToString(); DialogResult editResult = addressForm.ShowDialog(); if (editResult == DialogResult.OK) { editAddress.Name = addressForm.AddressName; editAddress.Address1 = addressForm.Address1; editAddress.Address2 = addressForm.Address2; editAddress.City = addressForm.City; editAddress.State = addressForm.State; editAddress.Zip = int.Parse(addressForm.ZipText); } addressForm.Dispose(); } } saForm.Dispose(); } }
//Precondition: Address menu item is clicked //Postcondition: An address is added to the UPV object's list and the dialog box is disposed. private void AddressToolStripMenuItem_Click(object sender, EventArgs e) { AddressForm addAddress = new AddressForm(); //new AddressForm object DialogResult dialogResult; //allows us to store a dialog result. dialogResult = addAddress.ShowDialog(); //displays the AddressForm. //checks to see if the dialogresult was set to ok. If it was, an address is added to the UPV object. if (dialogResult == DialogResult.OK) { upvObject.AddAddress(addAddress.NameValue, addAddress.Address1Value, addAddress.Address2Value, addAddress.CityValue, addAddress.StateValue, int.Parse(addAddress.ZipValue)); //passing the AddressForms results into the AddAddress method. addAddress.Dispose(); //Gets rid of the form. } }
//Precondition: AddressList must be > 0 //Postcondition: The address selected will be updated with newly specified values private void addressToolStripMenuItem1_Click(object sender, EventArgs e) { if (upv.AddressCount < SelectorForm.MIN_ADDRESSES) //Count of Addresses in the upv must be greater than 0 { MessageBox.Show("Need at least " + SelectorForm.MIN_ADDRESSES + " Address", "Address Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; // Exit now since can't create valid letter } SelectorForm Selector = new SelectorForm(upv.AddressList); // The Selector dialog box form DialogResult result = Selector.ShowDialog(); //Show Selector Form as dialog and store result AddressForm Editor = new AddressForm(); //The Editor dialog box form int addIndex = Selector.Index; //local variable to store the index selected in the Editor Form Editor.AddressName = upv.AddressAt(addIndex).Name; //Set Property to the Name of the AddressAt the SelectedIndex Editor.Address1 = upv.AddressAt(addIndex).Address1; //Set Property to the Address1 of the AddressAt the SelectedIndex Editor.Address2 = upv.AddressAt(addIndex).Address2; //Set Property to the Address2 of the AddressAt the SelectedIndex Editor.City = upv.AddressAt(addIndex).City; //Set Property to the City of the AddressAt the SelectedIndex Editor.State = upv.AddressAt(addIndex).State; //Set Property to the State of the AddressAt the SelectedIndex Editor.ZipText = $"{upv.AddressAt(addIndex).Zip}"; //Set Property to the Zip of the AddressAt the SelectedIndex if (result == DialogResult.OK) //if OK was clicked (Selector Form) { DialogResult result1 = Editor.ShowDialog(); //Show Editor form as a dialog box int zipResult; //integer holding TryParse result if (result1 == DialogResult.OK) //if OK was clicked (Editor Form) { upv.AddressAt(addIndex).Name = Editor.AddressName; //set upv Name property to the current value held in the Editor Forms TextBox upv.AddressAt(addIndex).Address1 = Editor.Address1; //set upv Address1 property to the current value held in the Editor Forms TextBox upv.AddressAt(addIndex).Address2 = Editor.Address2; //set upv Address2 property to the current value held in the Editor Forms TextBox upv.AddressAt(addIndex).City = Editor.City; //set upv City property to the current value held in the Editor Forms TextBox upv.AddressAt(addIndex).State = Editor.State; //set upv State property to the current value held in the Editor Forms TextBox if (int.TryParse(Editor.ZipText, out zipResult)) //Try parsing the value in the TextBox, if successful, set the upv Zip property to that value { upv.AddressAt(addIndex).Zip = zipResult; } } Editor.Dispose(); //dispose of resources Selector.Dispose(); //dispose of resources } }
//Precondition: click //Postcondition: Show Address Form, Add address to upv AddressList public void addressToolStripMenuItem_Click(object sender, EventArgs e) { AddressForm AF = new AddressForm(); //create an AddressForm object DialogResult result; //holds result of the AddressForm button (OK/Cancel) result = AF.ShowDialog(); //Shows the form as a modal dialog box if (result == DialogResult.OK && AF.ValidateChildren()) //if Result is OK, then Add an Address to the UserParcelView AddressList { //Precondition: ValidateChildren() must return true //Postcondition: Add Address to UserParcelView Address list upv.AddAddress(AF.NameValue, AF.AddressValue, AF.AddressValue1, AF.CityValue, AF.StateValue, AF.ZipValue); } else if (result == DialogResult.Cancel) //Dispose of resources if result == cancel { AF.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 { upv.AddAddress(addressForm.AddressName, addressForm.Address1, addressForm.Address2, addressForm.City, addressForm.State, int.Parse(addressForm.ZipText)); // Use form's properties to create address } 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: 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 int zip; // Address zip code if (result == DialogResult.OK) // Only add if OK { if (int.TryParse(addressForm.ZipText, out zip)) { upv.AddAddress(addressForm.AddressName, addressForm.Address1, addressForm.Address2, addressForm.City, addressForm.State, zip); // Use form's properties to create address } else // This should never happen if form validation works! { MessageBox.Show("Problem with Address Validation!", "Validation Error"); } } addressForm.Dispose(); // Best practice for dialog boxes // Alternatively, use with using clause as in Ch. 17 }
// 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 }
// 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(); }