Beispiel #1
0
        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                if (_hasEmailException)
                {
                    _message = string.Format(MessageBoxHelper.EXCEPTION_WRONG_EMAIL, txtEmail.Text);
                    throw new EmptyFieldException(_message);
                }
                switch (_currentProcess)
                {
                case CurrentProcess.Exception:
                    return;

                case CurrentProcess.New:
                    if (string.IsNullOrWhiteSpace(_newContact.Name))
                    {
                        throw new EmptyFieldException(string.Format(MessageBoxHelper.EXCEPTION_EMPTY_FIELD, "Full Name"));
                    }
                    if (string.IsNullOrWhiteSpace(_newContact.Email))
                    {
                        throw new EmptyFieldException(string.Format(MessageBoxHelper.EXCEPTION_EMPTY_FIELD, "Email"));
                    }
                    if (string.IsNullOrWhiteSpace(_newContact.Phone))
                    {
                        throw new EmptyFieldException(string.Format(MessageBoxHelper.EXCEPTION_EMPTY_FIELD, "Phone"));
                    }
                    if (!_hasDate)
                    {
                        _newContact.Birth = txtBirth.Value;
                    }
                    _controller.CreateContact(_newContact);
                    break;

                case CurrentProcess.Edition:
                    if (string.IsNullOrWhiteSpace(_controller.CurrentContact.Name))
                    {
                        throw new EmptyFieldException(string.Format(MessageBoxHelper.EXCEPTION_EMPTY_FIELD, "Full Name"));
                    }
                    if (string.IsNullOrWhiteSpace(_controller.CurrentContact.Email))
                    {
                        throw new EmptyFieldException(string.Format(MessageBoxHelper.EXCEPTION_EMPTY_FIELD, "Email"));
                    }
                    if (string.IsNullOrWhiteSpace(_controller.CurrentContact.Phone))
                    {
                        throw new EmptyFieldException(string.Format(MessageBoxHelper.EXCEPTION_EMPTY_FIELD, "Phone"));
                    }
                    if (!_controller.UpdateContact().Equals(1))
                    {
                        _message = string.Format(MessageBoxHelper.EXCEPTION_UPDATE_CONTACT, _controller.CurrentContact.Name);
                        throw new Exception(_message);
                    }
                    break;
                }
                DataGridViewConstructorList();
                ClearData();
                UnlockControls(true);
                _currentProcess = CurrentProcess.Init;
            }
            catch (EmptyFieldException efex)
            {
                MessageBox.Show(efex.Message, MessageBoxHelper.WARNING_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, MessageBoxHelper.ERROR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                if (_currentProcess.Equals(CurrentProcess.Edition))
                {
                    btnAdd.Text = "Add";
                }
                _currentProcess = CurrentProcess.Exception;
            }
        }