private void btnDebtDown_Click(object sender, EventArgs e)
        {
            int        index    = -1;
            List <int> toSelect = new List <int>();

            foreach (DataGridViewRow r in gvDebts.Rows)
            {
                index++;
                if (r.Selected && index < gvDebts.Rows.Count - 1)
                {
                    Debt dummy = debtList.Debts[index];
                    debtList.Debts[index]     = debtList.Debts[index + 1];
                    debtList.Debts[index + 1] = dummy;
                    toSelect.Add(index + 1);

                    DebtList.Save(debtList, path);
                }
            }

            RefreshData();

            foreach (int s in toSelect)
            {
                gvDebts.Rows[s].Selected = true;
            }
        }
        private void calculate_Click(object sender, EventArgs e)
        {
            frmDebt d = new frmDebt();

            d.ShowDialog();
            if (d.Value == DialogResult.OK)
            {
                Debt debt = new Debt(d.DebtName, d.Balance, d.IntrestRate, d.Payment, d.FirstPaymentDate);
                debtList.Debts.Add(debt);

                DebtList.Save(debtList, path);

                RefreshData();
            }

            /*
             * double rate = Convert.ToDouble(txtRate.Text);
             * double payment = Convert.ToDouble(txtPayment.Text);
             * double balance = Convert.ToDouble(txtBalance.Text);
             * string name = txtName.Text;
             * if (name.Trim() == "")
             * {
             *  name = "Debt " + (debtList.Debts.Count + 1).ToString();
             * }
             *
             * Debt debt = new Debt(name, balance, rate, payment);
             * debtList.Debts.Add(debt);
             *
             * DebtList.Save(debtList, path);
             *
             * RefreshData();
             */
        }
        private void btnCalculatePlan_Click(object sender, EventArgs e)
        {
            debtList.InitialSnowball      = Convert.ToDouble(txtExtraPayment.Text);
            debtList.InitialSnowballStart = dateTimePicker1.Value;
            DebtList.Save(debtList, path);

            RefreshData();
        }
        private void dgvExtraPayments_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 4)
            {
                bool check = Convert.ToBoolean(dgvExtraPayments.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);
                debtList.ExtraPayments[e.RowIndex].Enabled = check;

                DebtList.Save(debtList, path);

                RefreshData();
            }
        }
        private void frmMain_Load(object sender, EventArgs e)
        {
            debtList = new DebtList();
            comboBox1.SelectedIndex = 0;

            path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\debtsnowball.xml";
            if (File.Exists(path))
            {
                debtList = DebtList.Load(path);
                RefreshData();
            }
        }
        private void gvDebts_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            frmDebt d = new frmDebt(debtList.Debts[e.RowIndex]);

            d.ShowDialog();
            if (d.Value == DialogResult.OK)
            {
                Debt debt = new Debt(d.DebtName, d.Balance, d.IntrestRate, d.Payment, d.FirstPaymentDate);
                debtList.Debts[e.RowIndex] = debt;

                DebtList.Save(debtList, path);

                RefreshData();
            }
        }
        public static bool Save(DebtList dl, string Filename)
        {
            bool ret = false;

            try
            {
                Application.DoEvents();
                System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(dl.GetType());
                StreamWriter writer = new StreamWriter(Filename);
                x.Serialize(writer, dl);
                writer.Close();
                ret = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            return(ret);
        }
        private void btnDebtDelete_Click(object sender, EventArgs e)
        {
            int index = -1;

            foreach (DataGridViewRow r in gvDebts.Rows)
            {
                index++;
                if (r.Selected && index > -1)
                {
                    DialogResult dr = MessageBox.Show("Are you sure you want to delete the \"" + debtList.Debts[index].Name + "\" debt?", "Delete?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (dr == DialogResult.Yes)
                    {
                        debtList.Debts.RemoveAt(index);
                        DebtList.Save(debtList, path);
                    }
                }
            }

            RefreshData();
        }
        public static DebtList Load(string Filename)
        {
            DebtList ret = new DebtList();

            if (File.Exists(Filename))
            {
                try
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(DebtList));
                    TextReader    tr         = new StreamReader(Filename);
                    ret = (DebtList)serializer.Deserialize(tr);
                    tr.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

            return(ret);
        }
        private void dgvExtraPayments_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            frmExtraPayment f = new frmExtraPayment(debtList.ExtraPayments[e.RowIndex]);

            f.ShowDialog();
            if (f.Value == DialogResult.OK)
            {
                ExtraPayment ep = new ExtraPayment();
                ep.Name               = f.ExtraPaymentName;
                ep.Amount             = f.Amount;
                ep.Recurring          = f.Recurring;
                ep.StartDate          = f.StartDate;
                ep.StopDate           = f.StopDate;
                ep.IndefiniteStopDate = f.IndefiniteStopDate;

                debtList.ExtraPayments[e.RowIndex] = ep;

                DebtList.Save(debtList, path);

                RefreshData();
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            frmExtraPayment f = new frmExtraPayment();

            f.ShowDialog();
            if (f.Value == DialogResult.OK)
            {
                ExtraPayment ep = new ExtraPayment();
                ep.Name               = f.ExtraPaymentName;
                ep.Amount             = f.Amount;
                ep.Recurring          = f.Recurring;
                ep.StartDate          = f.StartDate;
                ep.StopDate           = f.StopDate;
                ep.IndefiniteStopDate = f.IndefiniteStopDate;

                debtList.ExtraPayments.Add(ep);

                DebtList.Save(debtList, path);

                RefreshData();
            }
        }
        public void RefreshData()
        {
            try
            {
                try
                {
                    txtExtraPayment.Text  = debtList.InitialSnowball.ToString();
                    dateTimePicker1.Value = debtList.InitialSnowballStart;
                }
                catch { }

                gvDebts.DataSource = null;
                gvDebts.DataSource = debtList.Debts;

                gvSnowball.DataSource = null;
                List <DebtPaymentDisplay> dpds = new List <DebtPaymentDisplay>();

                DebtList.SnowballApproaches sa = DebtList.SnowballApproaches.MyOrder;
                switch (comboBox1.SelectedIndex)
                {
                case 1: sa = DebtList.SnowballApproaches.LowestBalanceFirst;
                    break;

                case 2: sa = DebtList.SnowballApproaches.HighestInterestFirst;
                    break;
                }


                foreach (DebtPayment dp in debtList.ProcessPlan(true, sa, Convert.ToDouble(txtExtraPayment.Text), dateTimePicker1.Value))
                {
                    dpds.Add(new DebtPaymentDisplay(dp));
                }
                gvSnowball.DataSource = dpds;
                gvSnowball.FirstDisplayedScrollingRowIndex = gvSnowball.RowCount - 1;

                /* --------------------- */
                dgvNonSnowball.DataSource = null;
                List <DebtPaymentDisplay> dpds2 = new List <DebtPaymentDisplay>();
                foreach (DebtPayment dp in debtList.ProcessPlan(false, sa, Convert.ToDouble(txtExtraPayment.Text)))
                {
                    dpds2.Add(new DebtPaymentDisplay(dp));
                }
                dgvNonSnowball.DataSource = dpds2;
                dgvNonSnowball.FirstDisplayedScrollingRowIndex = dgvNonSnowball.RowCount - 1;
                /* -----------------------*/

                debtList           = DebtList.Load(path);
                gvDebts.DataSource = null;

                List <DebtDisplay> dds = new List <DebtDisplay>();
                foreach (Debt d in debtList.Debts)
                {
                    dds.Add(new DebtDisplay(d));
                }
                gvDebts.DataSource = dds;

                foreach (DataGridViewRow d in gvDebts.Rows)
                {
                    d.Selected = false;
                }

                dgvExtraPayments.DataSource = null;

                List <ExtraPaymentDisplay> epds = new List <ExtraPaymentDisplay>();
                foreach (ExtraPayment ep in debtList.ExtraPayments)
                {
                    epds.Add(new ExtraPaymentDisplay(ep));
                }

                dgvExtraPayments.DataSource = epds; //debtList.ExtraPayments;
                foreach (DataGridViewRow d in dgvExtraPayments.Rows)
                {
                    d.Selected = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }