Exemplo n.º 1
0
        // 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 (addressList.Count > 0)                                                // Only edit if there are addresses!
            {
                ChooseAddressForm chooseAddForm = new ChooseAddressForm(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 = addressList[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     = String.Format("{0:D5}", editAddress.Zip);

                        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;
                            try
                            {
                                editAddress.Zip = int.Parse(addressForm.ZipText);
                            }

                            catch (FormatException) // This should never happen if form validation works!
                            {
                                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");
            }
        }
Exemplo n.º 2
0
        // 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 (addressList.Count > 0) // Only edit if there are addresses!
            {
                ChooseAddressForm chooseAddForm = new ChooseAddressForm(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 = addressList[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 = String.Format("{0:D5}", editAddress.Zip);

                        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;
                            try
                            {
                                editAddress.Zip = int.Parse(addressForm.ZipText);
                            }

                            catch (FormatException) // This should never happen if form validation works!
                            {
                                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");
        }
Exemplo n.º 3
0
        // 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
        }
Exemplo n.º 4
0
        // 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
        }