private void btnAddConfirm_Click(object sender, EventArgs e)
        {
            bool areFieldsValid = ValidateInputFields();

            if (areFieldsValid)
            {
                string[] inputFields = new string[] {
                    this.nameTextBox.Text,
                    this.sinceDatePicker.Text,
                    this.dueDatePicker.Text,
                    this.phoneTextBox.Text,
                    this.emailTextBox.Text,
                    this.facebookTextBox.Text,
                    this.amountTextBox.Text
                };

                RegexOptions options = RegexOptions.None;
                Regex        regex   = new Regex("[ ]{2,}", options);

                for (int i = 0; i < inputFields.Length; i++)
                {
                    // Removing all unnecessary whitespaces.
                    inputFields[i] = inputFields[i].TrimStart().TrimEnd();
                    inputFields[i] = regex.Replace(inputFields[i], " ");
                }

                decimal currencyInterest   = decimal.Parse(this.interestWithCurrencyTextBox.Text);
                decimal percentageInterest = 1.00m + (decimal.Parse(this.interestWithPercentageTextBox.Text) / 100.00m);

                string  name     = inputFields[0];
                string  since    = inputFields[1];
                string  dueDate  = inputFields[2];
                string  phone    = inputFields[3];
                string  email    = inputFields[4];
                string  facebook = inputFields[5];
                decimal amount   = (decimal.Parse(inputFields[6]) + currencyInterest);

                if (percentageInterest > 0)
                {
                    amount *= percentageInterest;
                }

                string transactorType = string.Empty;
                string path           = TransactorsFilePath;

                if (btnDebtor.BackColor == Color.FromArgb(0, 208, 255))
                {
                    transactorType = TransactorType.Debtor.ToString();
                    XmlProcess.AddTransactorToXml(path, name, since, dueDate, phone, email, amount, facebook, transactorType);
                }
                else if (btnCreditor.BackColor == Color.FromArgb(0, 208, 255))
                {
                    transactorType = TransactorType.Creditor.ToString();
                    XmlProcess.AddTransactorToXml(path, name, since, dueDate, phone, email, amount, facebook, transactorType);
                }

                this.FormClosing -= AlertUserOnExit;
                this.Close();
            }
        }
Beispiel #2
0
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            bool areInputFieldsValid = AreInputFieldsValid(
                this.nameTextBox,
                this.sinceDatePicker,
                this.dueDatePicker,
                this.phoneTextBox,
                this.emailTextBox,
                this.facebookTextBox,
                this.amountTextBox,
                this.interestCheckBox,
                this.interestWithCurrencyTextBox,
                this.interestWithPercentageTextBox);

            if (areInputFieldsValid)
            {
                // The array is necessary so we can later trim each textbox.
                string[] inputFields = new string[] {
                    this.nameTextBox.Text,
                    this.sinceDatePicker.Text,
                    this.dueDatePicker.Text,
                    this.phoneTextBox.Text,
                    this.emailTextBox.Text,
                    this.facebookTextBox.Text,
                    this.amountTextBox.Text,
                    this.interestWithCurrencyTextBox.Text,
                    this.interestWithPercentageTextBox.Text
                };

                RegexOptions options = RegexOptions.None;
                Regex        regex   = new Regex("[ ]{2,}", options);

                for (int i = 0; i < inputFields.Length; i++)
                {
                    // Removing all unnecessary whitespaces.
                    inputFields[i] = inputFields[i].TrimStart().TrimEnd();
                    inputFields[i] = regex.Replace(inputFields[i], " ");
                }

                decimal currencyInterest   = decimal.Parse(inputFields[7]);
                decimal percentageInterest = 1.00m + (decimal.Parse(inputFields[8]) / 100.00m);

                this.newName        = inputFields[0];
                this.newSince       = inputFields[1];
                this.newDueDate     = inputFields[2];
                this.newPhoneNumber = inputFields[3];
                this.newEmail       = inputFields[4];
                this.newFacebook    = inputFields[5];
                this.newAmount      = decimal.Parse(inputFields[6]) + currencyInterest;
                Currency currencyObj = this.currencyComboBox.SelectedItem as Currency;
                this.newCurrency = currencyObj;

                if (percentageInterest > 1.00m)
                {
                    newAmount = decimal.Parse((newAmount * percentageInterest).ToString("f2"));
                }

                this.newTransactorType = string.Empty;
                XDocument xmlDocument = XDocument.Load(TransactorsFilePath);

                if (btnDebtor.BackColor == Color.FromArgb(0, 208, 255))
                {
                    this.newTransactorType = TransactorType.Debtor.ToString();
                }
                else if (btnCreditor.BackColor == Color.FromArgb(0, 208, 255))
                {
                    this.newTransactorType = TransactorType.Creditor.ToString();
                }

                if (this.oldTransactorType != this.newTransactorType)
                {
                    // Deleting the transactor from the old collection and putting it (with the new data) into the new collection.
                    xmlDocument.Root.Elements().FirstOrDefault(x => x.Attribute("no").Value == this.no.ToString() &&
                                                               x.Element("TransactorType").Value == this.oldTransactorType)
                    .Remove();

                    int noCounter = 1;
                    IEnumerable <XElement> transactorsWithType = xmlDocument
                                                                 .Element(XmlRoot)
                                                                 .Elements(XmlElement)
                                                                 .Where(x => x.Element("TransactorType").Value == this.oldTransactorType);

                    foreach (XElement debtor in transactorsWithType)
                    {
                        debtor.SetAttributeValue("no", noCounter++);
                    }

                    xmlDocument.Save(TransactorsFilePath, SaveOptions.DisableFormatting);

                    XmlProcess.AddTransactorToXml(TransactorsFilePath,
                                                  this.newName,
                                                  this.newSince,
                                                  this.newDueDate,
                                                  this.newPhoneNumber,
                                                  this.newEmail,
                                                  this.newAmount,
                                                  this.newCurrency.Abbreviation,
                                                  this.newFacebook,
                                                  this.newTransactorType);
                }
                else if (this.oldTransactorType == this.newTransactorType)
                {
                    // Overwriting the old data with the new one and keeping the record in the same collection.
                    XmlProcess.EditTransactorFromXml(
                        TransactorsFilePath,
                        this.no,
                        this.newName,
                        this.newSince,
                        this.newDueDate,
                        this.newPhoneNumber,
                        this.newEmail,
                        this.newFacebook,
                        this.newAmount,
                        this.newCurrency.Abbreviation,
                        this.oldTransactorType);
                }

                this.mainForm.EnableMainFormAndRefreshDataGrid(this.mainForm);
                this.FormClosing -= AlertUserOnExit;
                this.Close();
            }
        }
        private void btnAddConfirm_Click(object sender, EventArgs e)
        {
            bool areFieldsValid = ValidateInputFields();

            if (areFieldsValid)
            {
                string[] inputFields = new string[] {
                    this.nameTextBox.Text,
                    this.sinceDatePicker.Text,
                    this.dueDatePicker.Text,
                    this.phoneTextBox.Text,
                    this.emailTextBox.Text,
                    this.facebookTextBox.Text,
                    this.amountTextBox.Text
                };

                RegexOptions options = RegexOptions.None;
                Regex        regex   = new Regex("[ ]{2,}", options);

                for (int i = 0; i < inputFields.Length; i++)
                {
                    // Removing all unnecessary whitespaces.
                    inputFields[i] = inputFields[i].TrimStart().TrimEnd();
                    inputFields[i] = regex.Replace(inputFields[i], " ");
                }

                decimal currencyInterest   = decimal.Parse(this.interestWithCurrencyTextBox.Text);
                decimal percentageInterest = 1.00m + (decimal.Parse(this.interestWithPercentageTextBox.Text) / 100.00m);

                string  name     = inputFields[0];
                string  since    = inputFields[1];
                string  dueDate  = inputFields[2];
                string  phone    = inputFields[3];
                string  email    = inputFields[4];
                string  facebook = inputFields[5];
                decimal amount   = (decimal.Parse(inputFields[6]) + currencyInterest);

                if (percentageInterest > 0)
                {
                    amount *= percentageInterest;
                }

                string addTransactorType = string.Empty;
                string path = TransactorsFilePath;

                if (isEditable)
                {
                    if (btnDebtor.BackColor == Color.FromArgb(0, 208, 255))
                    {
                        addTransactorType = TransactorType.Debtor.ToString();
                    }
                    else if (btnCreditor.BackColor == Color.FromArgb(0, 208, 255))
                    {
                        addTransactorType = TransactorType.Creditor.ToString();
                    }

                    // If the user wants to edit data :
                    // If the user did not change the transactor type -
                    // I should keep the record at its place without changing the no.
                    // Otherwise (user has changed transactor type) I should remove the record from the previous
                    // collection and add it to the new one.

                    XDocument xmlDocument = XDocument.Load(TransactorsFilePath);

                    var transactorRoot = xmlDocument.Element("Transactors");

                    if (transactorRoot.HasElements)
                    {
                        XElement transactors = xmlDocument
                                               .Element("Transactors")
                                               .Elements("Transactor")
                                               .Where(x => x.Element("TransactorType").Value == this.transactorType &&
                                                      x.Attribute("no").Value == this.no.ToString())
                                               .FirstOrDefault();

                        if (transactorRoot != null)
                        {
                        }
                    }
                }
                else
                {
                    // If the user wants to add data.
                    if (btnDebtor.BackColor == Color.FromArgb(0, 208, 255))
                    {
                        addTransactorType = TransactorType.Debtor.ToString();
                        XmlProcess.AddTransactorToXml(path, name, since, dueDate, phone, email, amount, facebook, addTransactorType);
                    }
                    else if (btnCreditor.BackColor == Color.FromArgb(0, 208, 255))
                    {
                        addTransactorType = TransactorType.Creditor.ToString();
                        XmlProcess.AddTransactorToXml(path, name, since, dueDate, phone, email, amount, facebook, addTransactorType);
                    }
                }

                this.FormClosing -= AlertUserOnExit;
                this.Close();
            }
        }
Beispiel #4
0
        private void btnAddConfirm_Click(object sender, EventArgs e)
        {
            bool areInputFieldsValid = AreInputFieldsValid(
                this.nameTextBox,
                this.sinceDatePicker,
                this.dueDatePicker,
                this.phoneTextBox,
                this.emailTextBox,
                this.facebookTextBox,
                this.amountTextBox,
                this.interestCheckBox,
                this.interestWithCurrencyTextBox,
                this.interestWithPercentageTextBox);

            if (areInputFieldsValid)
            {
                // The array is necessary so we can later trim each textbox.
                string[] inputFields = new string[] {
                    this.nameTextBox.Text,
                    this.sinceDatePicker.Text,
                    this.dueDatePicker.Text,
                    this.phoneTextBox.Text,
                    this.emailTextBox.Text,
                    this.facebookTextBox.Text,
                    this.amountTextBox.Text
                };

                RegexOptions options = RegexOptions.None;
                Regex        regex   = new Regex("[ ]{2,}", options);

                for (int i = 0; i < inputFields.Length; i++)
                {
                    // Removing all unnecessary whitespaces.
                    inputFields[i] = inputFields[i].TrimStart().TrimEnd();
                    inputFields[i] = regex.Replace(inputFields[i], " ");
                }

                decimal currencyInterest   = decimal.Parse(this.interestWithCurrencyTextBox.Text);
                decimal percentageInterest = 1.00m + (decimal.Parse(this.interestWithPercentageTextBox.Text) / 100.00m);

                string   name                 = inputFields[0];
                string   since                = inputFields[1];
                string   dueDate              = inputFields[2];
                string   phone                = inputFields[3];
                string   email                = inputFields[4];
                string   facebook             = inputFields[5];
                decimal  amount               = (decimal.Parse(inputFields[6]) + currencyInterest);
                Currency currencyObj          = this.currencyComboBox.SelectedItem as Currency;
                string   currencyAbbreviation = currencyObj.Abbreviation;

                if (percentageInterest > 1.00m)
                {
                    amount = decimal.Parse((amount * percentageInterest).ToString("f2"));
                }

                string addTransactorType = string.Empty;

                if (btnDebtor.BackColor == DefaultButtonColor)
                {
                    addTransactorType = TransactorType.Debtor.ToString();
                    XmlProcess.AddTransactorToXml(TransactorsFilePath, name, since, dueDate, phone, email, amount, currencyAbbreviation, facebook, addTransactorType);
                }
                else if (btnCreditor.BackColor == DefaultButtonColor)
                {
                    addTransactorType = TransactorType.Creditor.ToString();
                    XmlProcess.AddTransactorToXml(TransactorsFilePath, name, since, dueDate, phone, email, amount, currencyAbbreviation, facebook, addTransactorType);
                }

                this.FormClosing -= AlertUserOnExit;
                this.Close();
            }
        }